Attributes router is a light library allowing to set up a router and to define routes via the attributes of PHP 8.
Before, you can download the library you must first have a version of PHP ~8.0 and a recent version of Composer.
-
First, as the library is not referenced on Packagist, you will have to add the repository in the configuration of your composer.json
"repositories": [ { "type": "vcs", "url": "https://github.com/Eredost/attributes-router.git" } ]
-
Then, all you have to do is install the library using the following command:
composer require eredost/attributes-router
Simple usage of the router:
-
Define the routes in your controller with the Route attribute
<?php namespace App\Controller; use AttributesRouter\Attribute\Route; class MainController { #[Route('/', name: 'homepage', methods: ['GET', 'POST'])] public function home() { } #[Route('/article/{slug}/comment/{id<\d+>}', name: 'article-comment')] public function comment() { } }
-
Create the router passing as argument the controllers on which you have defined routes attributes.
<?php use App\Controller\MainController; use AttributesRouter\Router; require 'vendor/autoload.php'; $router = new Router([MainController::class]); // If there is a match, he will return the class and method associated // to the request as well as route parameters if ($match = $router->match()) { $controller = new $match['class'](); $controller->{$match['method']}($match['params']); }
You have the possibility after instantiating the Router object to be able to add new controllers, these will be added with those already stored.
$router->addRoutes([AnotherController::class]);
It can be interesting in certain cases, such as for example when your project is called from a sub-directory, to define a base URI so that this one is ignored when the router compares the routes with the current request. You can define it either via the constructor or via the setter.
// Via the Router constructor
$router = new Router([MainController::class], '/dir/sub-dir');
// Via the associated setter
$router->setBaseURI('/dir/sub-dir');
You have the possibility from the name of the route, to generate a URL.
$router->generateUrl('article-comment', ['slug' => 'hello-world', 'id' => 15]);
See the Contributing.md file for more information on how to contribute to the project.