router / slim
Slim Router
Installs: 921
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 2
Type:application
pkg:composer/router/slim
Requires
- php: ^7.2||^8.0
- guzzlehttp/psr7: ^2.0
- http-interop/http-factory-guzzle: ^1.0
- laminas/laminas-diactoros: ^2.2
- nyholm/psr7: ^1.0
- nyholm/psr7-server: ^1.0
- slim/http: ^1.0
- slim/psr7: ^1.0
- slim/slim: ^4.0
Requires (Dev)
- phpunit/phpunit: ^10.0.19
README
application with Slim Framework
Installation
Use Composer
Do Composer Require as below
composer require router/slim {version}
Create instance
use \Cs\Router\Util\App as Router;
$app = new Router(
$containerServices,
$routes, $cors -> [array] optional
);
$app->run();
Note: The router will support GET|POST|PUT|OPTIONS alone
Create a routes as below and send the routes as array.
-
url: /test[/{return}] (optional)
method: get|post|put|head|delete
routeBeforeInvoke:
- [class]->[function]
...
invoke: [class]->[function]
return: json|raw|download (*optional)
urlis the path of routemethodis http method like get|post. you can specify it either small or capsrouteBeforeInvokeis a special routing like middleware(optional). this is will be invoked before actual method call and the response will to forwared with method|function name asarray.invokeis callback placeholder.classis a service name andfunctionis callback method. If you were using DI container then provide full path of the service name.[/{return}]is a optional routing, which is used to return a response asjson,raw,download. You can specify it either uri routing or explicitly in array as mentioned above.downloadis will force the response as downloadable request. Note* its return values should be like below in the response section.
Your service Response should like below
If return type is Download then the method response should be Array like below
[
'file' => File Content,
'fileSize' => File Size,
'ContentType' => mime,
'fileName' => FileName (in what name it should be downloaded),
'statusCode' => integer (optional)
]
If return type is Download then the method response should be array like below
[
'statusCode' => integer (optional),
'data' => data [array]
]
If return type is raw then the method response should be a string like below
'response' -> (string)
Example 1
##### specified, routing level return type & routeBeforeIvoke is a kind of middleware it should be called before actual routing method and their reponses are returned in their own method name.
-
url: /test[/{return}]
routeBeforeInvoke:
- test->Validate
- test->Validate2
method: post
invoke: test->welcome
return: json
req -> /test/json
* test->Validate get called returns ['Validate' => [response]]
* test->Validate get called returns ['Validate' => [response], 'Validate2' => [response]]
* test->welcome finally called your responses and it should be array of thing bcz, return type is specified as json * [/{return}] -> json *
Note* static return type will be overrided by dynamic routing (URI routing).
-
url: /test/{token}[/{return}]
method: get
invoke: test->getData
-
url: /test/download[/{return}] (You should specify return is download)
method: get
invoke: test->getFile
Cors values
cors:
allow_credentials: 'true'
accept_headers: Content-Type, X-Requested-With
origin:
- http://domain.name.com
Router request body should like below
Request body -> GET
uri: test/{name}?job=test
method: get
[
'headers' => [
'headers'
],
'params' => [
'name' => 'chella',
'job' => 'test'
]
]
Request body -> POST
uri: test/{name}
method: post
[
'data' => [
'post data'
],
'headers' => [
'headers'
],
'params' => [
'name' => 'chella
]
]
Request body -> PUT
uri: test/{name}
method: put
[
'data' => 'stream',
'headers' => [
'headers'
],
'params' => [
'name' => 'chella
]
]
Request body -> POST
uri: test/{name}
method: post (fileupload)
[
'data' => [
'postdata'
],
'headers' => [
'headers'
],
'params' => [
'name' => 'chella
],
'files' => [
[
'file' => fileStream,
'name' => fileName,
'mime' => mediaType,
'size' => size
]
[
...
]
]
]
Example
<?php
require_once 'vendor/autoload.php';
use EqnComparer\Util\Container;
use \Cs\Router\Util\App as Router;
$yaml = __DIR__ . '/app/Tnq/EqnComparer/Config/Routes.yaml';
$appYaml = __DIR__ . '/app/Tnq/EqnComparer/Config/Config.yaml';
$context = Container::getContext(
['routesConfig' => $yaml, 'appConfig' => $appYaml]
);
$routes = $context['routesConfig'];
$cors = $context['appConfig']['cors'];
try {
$app = new Router([], $context, $routes, $cors);
$app->run();
}
catch(Exception $e) {
echo $e->getMessage();
}
Stargazers over time
Here you go!
☺ Happy routing