Download the PHP package njeaner/router without Composer
On this page you can find all versions of the php package njeaner/router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package router
PHP Simple Fast Router
A simple PHP fast router
Installation
How to use it
Router and Routes Initialization
Initialization without ContainerInterface parameter
Initialization with ContainerInterface parameter
For complexe projects that need a container to resolve route callback, route middlewares or route policy arguments, you can initialize router passing an instance of \Psr\Container\ContainerInterface. by default, njeaner\router package recommand njeaner\container-di package that was used to process package tests; but you can use any other container package that implements \Psr\Container\ContainerInterface.
Register routes
any of these methods beyond return and instance of \Njeaner\Router\Routes\Route
Route name
Route name argument can be null or empty, then route can will be used as route name
Route path
Route path accept parameters that can be indicated using to format:
-
semicolon format. In this case, Njeaner\Router\Routes\Route::with() function can be used to indicate regex format of this path parameter
- bracket format.
Route callback
Route callback accept several formats:
- callback function
- A invokable controller classname.
- A string combining controller classname and controller action name joined by @ or #.
- A array containing controller classname and controller action name
Route resolution
\Njeaner\Router\Router::resolve() function allow user to process some request object to retrieve matched route previously registered in \Njeaner\Router\Router instance. This method accept two arguments
- an instance of \Psr\Http\Message\ServerRequestInterface, $request. by default, njeaner/router recommand guzzlehttp/psr7 package
- an boolean argument, $throwException, that permit to return null or throw an exception if request path does not match any router register route.
Resolving and returning matched route allow user to decide the moment that he want to process route callback function.
Processing route callback function (run route callback)
\Njeaner\Router\Router::run() function allow user to process matched route callback function. This method accept one argument: an instance of \Psr\Http\Message\ServerRequestInterface, $request.
route callback can return any type of value (string, array, stringable object, psr7 response object), but returned value will be convert by \Njeaner\Router\Router::run() function to a \Psr\Http\Message\ResponseInterface instance
By default, Njeaner\Router\Routes\Route instance is able to resolve route path parameters, convert these parameters as route attributes, inject these attributes and request instance into route callback function if that is necessary.
For most complex route callback arguments resolution, the injection of a container instance to router instance is required.
Route middlewares
route middlewares are called and processed before processing route callback function. This allow user to perform some action when some route match current request path. Route middleware can be a callable function, an invokable class, an array containing middleware classname with it method name, or a name of \Psr\Http\Server\MiddlewareInterface class.
middleware is set to route using \Njeaner\Router\Router::middleware() function or \Njeaner\Router\Router::middlewares() to set several route middlewares. Middleware callable function accept to arguments:
- an $request argument that is an instance of \Psr\Http\Message\ServerRequestInterface;
- an $next (or $handler for \Psr\Http\Server\MiddlewareInterface) argument that is really the matched route instance. Thus, Njeaner\Router\Routes\Route implements Psr\Http\Server\RequestHandlerInterface and is an invokable class.
NOTE: last middleware response is converted to \Psr\Http\Message\ResponseInterface.
Route policy
Route policy can be a boolean value, a callable classname, a callable function or an array containing Policy classname with it callable Method. Policy allow execution of \Njeaner\Router\Router::run() function only if it value or processed value is true.
All versions of router with dependencies
psr/http-server-middleware Version ^1.0
psr/http-message Version ^2.0
psr/container Version ^2.0