Download the PHP package webshr/router without Composer
On this page you can find all versions of the php package webshr/router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download webshr/router
More information about webshr/router
Files in webshr/router
Package router
Short Description A custom PHP router for the WP HTMX plugin, based on the Rareloop router package
License MIT
Homepage https://github.com/webshr/router
Informations about the package router
Webshr Router
A custom PHP Router for the WP HTMX WordPress plugin based on Rareloop Router.
Installation
Usage
Creating Routes
Map
Creating a route is done using the map
function:
map()
takes 3 parameters:
methods
(array): list of matching request methods, valid values:GET
POST
PUT
PATCH
DELETE
OPTIONS
uri
(string): The URI to match againstaction
(function|string): Either a closure or a Controller string
Route Parameters
Parameters can be defined on routes using the {keyName}
syntax. When a route matches that contains parameters, an instance of the RouteParams
object is passed to the action.
If you need to add constraints to a parameter you can pass a regular expression pattern to the where()
function of the defined Route
:
Optional route Parameters
Sometimes your route parameters needs to be optional, in this case you can add a ?
after the parameter name:
Named Routes
Routes can be named so that their URL can be generated programatically:
If the route requires parameters you can be pass an associative array as a second parameter:
If a passed in parameter fails the regex constraint applied, a RouteParamFailedConstraintException
will be thrown.
HTTP Verb Shortcuts
Typically you only need to allow one HTTP verb for a route, for these cases the following shortcuts can be used:
Setting the basepath
The router assumes you're working from the route of a domain. If this is not the case you can set the base path:
Controllers
If you'd rather use a class to group related route actions together you can pass a Controller String to map()
instead of a closure. The string takes the format {name of class}@{name of method}
. It is important that you use the complete namespace with the class name.
Example:
Creating Groups
It is common to group similar routes behind a common prefix. This can be achieved using Route Groups:
Middleware
PSR-15/7 Middleware can be added to both routes and groups.
Adding Middleware to a route
At it's simplest, adding Middleware to a route can be done by passing an object to the middleware()
function:
Multiple middleware can be added by passing more params to the middleware()
function:
Or alternatively, you can also pass an array of middleware:
Adding Middleware to a group
Middleware can also be added to a group. To do so you need to pass an array as the first parameter of the group()
function instead of a string.
You can also pass an array of middleware if you need more than one:
Defining Middleware on Controllers
You can also apply Middleware on a Controller class too. In order to do this your Controller must extend the Webshr\Router\Controller
base class.
Middleware is added by calling the middleware()
function in your Controller's __constructor()
.
By default all Middleware added via a Controller will affect all methods on that class. To limit what methods Middleware applies to you can use only()
and except()
:
Matching Routes to Requests
Once you have routes defined, you can attempt to match your current request against them using the match()
function. match()
accepts an instance of Symfony's Request
and returns an instance of Symfony's Response
:
Return values
If you return an instance of Response
from your closure it will be sent back un-touched. If however you return something else, it will be wrapped in an instance of Response
with your return value as the content.
Responsable objects
If you return an object from your closure that implements the Responsable
interface, it's toResponse()
object will be automatically called for you.
404
If no route matches the request, a Response
object will be returned with it's status code set to 404
;
Accessing current route
The currently matched Route
can be retrieved by calling:
If no route matches or match()
has not been called, null
will be returned.
You can also access the name of the currently matched Route
by calling:
If no route matches or match()
has not been called or the matched route has no name, null
will be returned.
Using with a Dependency Injection Container
The router can also be used with a PSR-11 compatible Container of your choosing. This allows you to type hint dependencies in your route closures or Controller methods.
To make use of a container, simply pass it as a parameter to the Router's constructor:
After which, your route closures and Controller methods will be automatically type hinted:
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
Credits
License
MIT
All versions of router with dependencies
altorouter/altorouter Version ^2.0
laminas/laminas-diactoros Version ^3.3
mindplay/middleman Version ^4.0
php-di/php-di Version ^7.0
psr/container Version ^2.0
psr/http-message Version ^2.0
psr/http-server-middleware Version ^1.0
spatie/macroable Version ^2.0