Download the PHP package marceauka/router without Composer
On this page you can find all versions of the php package marceauka/router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package router
Router
Router is a lightweight HTTP resquest router written in PHP.
It will be your perfect companion if you need a simple and effective library or if you want an easy to understand routing library.
Otherwise, take a look at these awesome libraries symfony/routing ou league/route.
Installation
Router can be installed with composer: composer require marceauka/router
.
For now, it requires PHP 7.4 or 8.0.
Also, copy / paste src/Router.php where you want and require it.
Configure Apache
The router works perfectly with any kind of URL, but if you want some url rewriting, you can use this example .htaccess.
Usage
Instanciation
Router as no params.
Adding routes
Once you have the instance, adding routes is childish.
Your routes can follow these HTTP verbs: POST, PUT, PATCH and DELETE.
A route can use many HTTP verbs:
Finally, routes can be chained or added with a callback:
If no callback is given to the routes() method, all routes will be returned.
Listening requests
For everything to work, Router needs to incoming requests:
Dispatching actions
Obviously, for each route, a need. You need to define an action for each of them.
-
A callback :
- A class (controller, ...) :
Here, the route, once matched, will instanciate the class named "MyClass" and will call the "myMethod" method.
Note: Router will accepts namespaces if you application can autoload them (PSR-4, ...).
Besides, you can define a global namespace for all of your actions.
You can define a not found action when no routes was matched.
URL parameters
Your routes can contains dynamic parameters. Usage is simple.
Parameters can be:
- {:num} for a numeric value
- {:alpha} for a alpha value
- {:any} for all kinds of chars
- {:slug} for numbers, alpha, dash (-) and arobase (@)
Once matched, parameters are sent to the corresponding action in the URL defined order.
Named routes
You can give a name to your routes to access them later or easily creating links (in a view for example).
If your route contains parameters, you can build an URI with filled parameters.
You need to give all parameters expected by the route, otherwise and exception will be rised.
Caching
Sometimes, when our router contains many routes, it's convenient to have a ready-to-use Router instance for each script execution. Router supports serialization and unserialization. Two helpers exists to assists you.
-
Export the configured instance:
- Import a configured instance:
Note: Routes using a callback can't be serialized. Only the "MyClass@myMethod" is serializable.
The router does not provide functionnality to store or read a cache file. It's not its goal.
Tests
Tests are with PHPUnit 9. You can use the phpunit command given at vendor/bin.
Tests are certainly incomplete. Feel free to contribute.
Licence
MIT