Download the PHP package mrcl/slim-routes without Composer
On this page you can find all versions of the php package mrcl/slim-routes. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mrcl/slim-routes
More information about mrcl/slim-routes
Files in mrcl/slim-routes
Informations about the package slim-routes
SlimRoutes
PHP8 attributes for easier and cleaner routing in Slim.
Table of contents
- Installation
- Usage
- Attributes
- Configuration
- Further Information
- Route patterns
- Middleware
- HTTP methods
- Grouping routes
- API versioning
Installation
SlimRoutes requires >= PHP 8.1
Usage
Attributes
Controller
#[Controller]
marks a controller class as routable.
Controller parameters
Parameter | Description | ||
---|---|---|---|
pattern | Prefixes all routes' pattern | ||
middleware | Adds middleware to all routes | ||
version | Specify the basic API version(s) for all routes Can get overriden by route |
||
groupId | Group all routes by specific group configuration Can get overriden by route |
Route
#[Route]
maps a route to an action.
By default, it uses the GET
method.
Route parameters
Parameter | Description |
---|---|
pattern | Route pattern |
method | HTTP method(s) |
middleware | Route middleware |
version | Specify the route's API version(s) |
groupId | Use specific group configuration |
priority | Prioritize route |
name | Unique route name |
Configuration
Minimal configuration
For a minimal configuration you only need to pass an instance of the Slim/App
(or any other class which
implements Slim\Interfaces\RouteCollectorInterface
or Slim\Interfaces\RouteCollectorProxyInterface
) and directories
where your action/controller classes are located.
Note:
#[Controller(version, groupId)]
and#[Route(version. groupId, priority)]
do not work without configuration.
Configuration options
Caching
Recommended for production usage.
Additional directory
Add another directory to search for routable classes.
File name pattern
You can minimize the amount of scanned classes by setting a file name/extension pattern (regex).
Recommended if you have a lot of other classes in your folders and/or you do not use caching (e.g. in development).
Example:
All wanted file names end with 'Controller' and file extensions are 'php' or 'PHP'
Options for *Action.php
and *Controller.php
files are ready to use.
Groups
For more details, see Advanced grouping with groups.
API version
For more details, see API versioning.
Route priority
If you want to prioritize your routes, you need to enable it first. Predefined constants are available
in RoutePriority
.
If you plan to use your own range of priorities, you can pass a defaultPriority
. Priorities are simple integers, the
lower the number the higher the priority (better position in the route stack).
Change mapping of ANY
The special HttpMethod::ANY
maps "any" HTTP method to your route.
By default, it maps to GET
, POST
, PUT
, PATCH
, DELETE
and OPTIONS
(like in Slim).
You can configure it to your own needs:
Further information
Route patterns
Leading slash
You may have noticed that all route patterns in this documentation do not use a leading /
. They are automatically
added on route generation.
If you prefer to use leading slashes, just use them.
Pattern order
Depending on Slim configuration a base path or group can be the first pattern element.
[/Slim][/ApiVersion][[/Parent...Group]/Group][/Controller]/Route
Middleware
Unlike in Slim added middleware runs in the order you set it.
Request > FirstMiddleware > SecondMiddleware > ThirdMiddleware > FourthMiddleware
UserController:getAllUsers
FourthMiddleware > ThirdMiddleware > SecondMiddleware > FirstMiddleware > Response`
Middleware order
Added middleware on Slim level is always the first to be run.
[SlimMiddleware][ApiVersionMiddleware][[ParentGroup...Middleware]GroupMiddleware][ControllerMiddleware][RouteMiddleware]
HTTP methods
SlimRoutes comes with predefined constants of the most used HTTP methods.
Also see, Change mapping of ANY.
Grouping routes
Controller pattern
By using the #[Controller]
attribute you have the option to pass a pattern
which prefixes all routes within the
class.
Advanced grouping with groups
If you want to simply group action classes or have a more complex route setup and do not want to reassign patterns and middleware all the time, you can configure groups to use in your routes.
You can use an extra class for defining GroupConfiguration
s.
Add groups to SlimRoutes
Pass the group's ID
API versioning
For enabling API versioning to all of your routes, you have to configure
an VersionConfiguration
.
Multiple API versions
Let's assume you have three API versions v1
, v2
, v3
.
- v1 is only for some legacy routes, e.g.
/v1/updates
- v3 is the latest and just went live, users are in the process of updating
- v2 is still used by the majority of users
A possible configuration could look like the following:
Route order
The route stack would contain routes in the following order
You can still use the priority
argument on routes to lower or heighten their position.
Versioning for specific routes
We have the following additional config
For a controller setup a possible config could be:
For action classes:
Unversioned routes
Exclude only some routes
If you want to exclude some routes from versioning you can do so by pass VersionConfiguration::NONE
to the #[Route]
or #[Controller]
attribute.
Additionally, if all of your unversioned routes need some specific middleware you can add a VersionConfiguration
.
Make all routes available without version
If you have the special case that all routes (without specific version assignment) need also to be accessed without versioning you can do so
Versioned route names
When you add an API version and use route names, route names will be prefixed with the added version(s).
Route names are v1-my-action
and v2-my-action