Download the PHP package ergy/slim-annotations-router without Composer
On this page you can find all versions of the php package ergy/slim-annotations-router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ergy/slim-annotations-router
More information about ergy/slim-annotations-router
Files in ergy/slim-annotations-router
Package slim-annotations-router
Short Description Controller and annotations based router for Slim Framework V3
License MIT
Informations about the package slim-annotations-router
Slim Annotations Router
Controller and annotations based router for Slim Framework V3
Installation
Via Composer :
Initialization
To initialize the annotations router, simply add this lines to your index.php file that loads your Slim application :
If your application contains controllers in multiple folders, you can add them by passing an array in the second parameter to the constructor instead of a string, eg :
Controller files
The annotations router detects all files with names ending in "Controller.php" and all public methods with names ending in "Action". It then parses their annotations to generate routes recognized by Slim framework.
The following annotations are supported :
Name | Level | Type | Required | Description |
---|---|---|---|---|
@RoutePrefix | Class | string | no | The route prefix, used for all routes in the current class |
@Route | Method | Object | yes | The route definition |
The @Route object accepts the following syntaxes :
Let's see an example :
By opening the url http://your_site_url/home/hello/foobar, you should see "Hello foobar !".
Get Slim dependency container
Once your controller loaded, you might need to interact with the current HTTP request and response, to get theses objects, your controller simply has to extends the Ergy\Slim\Annotations\Controller class.
Extending this class allows you to retrieve a reference to the Slim dependency container, so to retrieve the current request, you simply have to ask for $this->request.
Let's see an example with the previous class :
Since we have a reference to the Slim dependency container, we can retrieve any object it contains, for example if you want to retrieve the applications settings, simply ask for $this->settings.
Routing events
The annotations router exposes the methods beforeExecuteRoute and afterExecuteRoute.
These methods take as a parameter a Ergy\Slim\Annotations\RouteInfo instance that gives you information about the running route.
Implementing these methods in your controller (or one of its parent classes) allow you to implement hook points before/after the actions are executed.
Example with the previous class :
By opening the url http://your_site_url/home/hello/foobar, you should see "before Hello foobar ! after".
Operation cancellation
Previous hooks allow you to cancel operations if needed :
-
You can cancel execution of current route and afterExecuteRoute method by returning either false or an instance of \Psr\Http\Message\ResponseInterface in the method beforeExecuteRoute.
- You can cancel the call to afterExecuteRoute method by returning either false or an instance of \Psr\Http\Message\ResponseInterface in the current route method.
For example :
By opening the url http://your_site_url/home/hello/foobar, you should see "before Hello foobar !".