Download the PHP package adinan-cenci/router without Composer
On this page you can find all versions of the php package adinan-cenci/router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package router
A small PHP router library
A simple PHP router to handle http requests.
This library is PSR-15 compliant and therefore works with PSR-7 messages and makes use of PSR-17 factories.
Instantiating
Adding routes
You may add routes by informing the http method, the regex pattern to be matched against the the path and the associated controller:
Http method
Regex patterns
A simple regex pattern. Capture groups will be passed to the controller as attributes.
Obs: The route accepts multiple patterns as an array.
Controllers
The controller will receive two paramaters: an instance of Psr\Http\Message\ServerRequestInterface
and Psr\Http\Server\RequestHandlerInterface
( the router itself ) respectively.
By default, the routes accept various arguments as controllers:
Those are the default controller types, see the contents of the "examples" directory for more detailed examples.
Check out the advanced section below to learn how to add support for new types.
Obs: If the controller does not exist or cannot be called because of some reason or another, an exception will be thrown.
::add() shorthands
Middlewares
Middlewares will be processed before the routes.
Middlewares are similar to routes but unlike routes more than one middleware may be executed.
Executing
Calling ::run()
will execute the router and send a respose.
Error handling
Exceptions
By default, catched exceptions will be rendered in a 500 response object, you may customize it by setting your own handler.
Not found
By default when no route is found, the router will render a 404 response object, you may customize it by setting your own handler.
PSR compliance and niceties
This library is PSR-15 compliant, as such your controllers may tailor the response in details as specified in the PSR-7.
IMPORTANT
If your controller does not return an instance of ResponseInterface
, the router will create a generic response based out of whatever was outputed through echo
and print
.
Besides the defaults, the router offers some niceties.
::setDefaultNamespace($namespace)
Set the default namespace, so there will be no need to write the entire class name of the controller when defining routes.
Factories
The handler makes available a PSR-17 response and stream factories to make creating responses more convenient.
In this spirit of making things easier, the default response factory comes with a series of useful methods:
Cookies
Similarly the response objects have the ::withAddedCookie()
:
Advanced
Working inside sub-directories
The router will automatically work inside sub-directories.
Consider the example:
Your URL: http://yourwebsite.com/foobar/about
Your document root is
/var/www/html/
and your router is inside of
/var/www/html/foobar/
.
The router will match the routes against about
and NOT foobar/about
.
Still, if you really need to work with foobar/about
, then you must pass /var/www/html/
as your base directory to the Router
's constructor.
Factories
The constructor's second and third parameters are a PSR-17 response and stream factories respectively.
If not provided, generic implementations will be used.
Those factories will made available to the controllers, as explained earlier.
Custom controller types
The constructor's fourth parameter is an instance of AdinanCenci\Router\Caller\CallerInterface
, it is the object that will execute the controllers.
If not provided, the default caller will be used.
This default caller makes use of several implementations of AdinanCenci\Router\Caller\Handler\HandlerInterface
to support the different types of controllers listed earlier.
You can write your own handlers and callers to support your own version of a controller.
Dependency injection
So the router supports classes as controllers, how are they instantiated ?
The default ClassHandler
and MethodHandler
depend on an instance of AdinanCenci\Router\Instantiator\InstantiatorInterface
.
The default implementation simply calls new
to instantiate them.
If you wish use automatic dependency injection, you will need to provide your own caller/handler/instantiator.
How dependency injection is handled is beyond the escope of the library.
Server configuration
In order for it to work, we need to rewrite the requests to the file containing our router. Below are some examples:
Apache
Here is the example of a .htaccess for Apache:
Nginx
Here is the example for nginx:
IIS
Here is the example of a web.config for Microsoft IIS:
Installing
Use composer
License
MIT
All versions of router with dependencies
adinan-cenci/psr-17 Version ^1.0.0
psr/http-server-handler Version 1.0.x-dev
psr/http-server-middleware Version 1.0.x-dev