application with Slim Framework
Use Composer
composer require router/slim {version}
use \Cs\Router\Util\App as Router;
$app = new Router(
$containerServices,
$routes, $cors -> [array] optional
);
$app->run();
-
url: /test[/{return}] (optional)
method: get|post|put|head|delete
routeBeforeInvoke:
- [class]->[function]
...
invoke: [class]->[function]
return: json|raw|download (*optional)
url
is the path of routemethod
is http method like get|post. you can specify it either small or capsrouteBeforeInvoke
is 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
.invoke
is callback placeholder.class
is a service name andfunction
is 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.download
is will force the response as downloadable request. Note* its return values should be like below in the response section.
[
'file' => File Content,
'fileSize' => File Size,
'ContentType' => mime,
'fileName' => FileName (in what name it should be downloaded),
'statusCode' => integer (optional)
]
[
'statusCode' => integer (optional),
'data' => data [array]
]
'response' -> (string)
##### 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:
allow_credentials: 'true'
accept_headers: Content-Type, X-Requested-With
origin:
- http://domain.name.com
uri: test/{name}?job=test
method: get
[
'headers' => [
'headers'
],
'params' => [
'name' => 'chella',
'job' => 'test'
]
]
uri: test/{name}
method: post
[
'data' => [
'post data'
],
'headers' => [
'headers'
],
'params' => [
'name' => 'chella
]
]
uri: test/{name}
method: put
[
'data' => 'stream',
'headers' => [
'headers'
],
'params' => [
'name' => 'chella
]
]
uri: test/{name}
method: post (fileupload)
[
'data' => [
'postdata'
],
'headers' => [
'headers'
],
'params' => [
'name' => 'chella
],
'files' => [
[
'file' => fileStream,
'name' => fileName,
'mime' => mediaType,
'size' => size
]
[
...
]
]
]
<?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();
}
☺ Happy routing