Skip to content

PHP 8 attributes based router supporting dynamic routes and reverse routing

License

Notifications You must be signed in to change notification settings

Eredost/attributes-router

Repository files navigation

Attributes router

Build Status Maintainability Test Coverage

Attributes router is a light library allowing to set up a router and to define routes via the attributes of PHP 8.

Installation

Before, you can download the library you must first have a version of PHP ~8.0 and a recent version of Composer.

  1. 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"
       }
    ]
  2. Then, all you have to do is install the library using the following command:

    composer require eredost/attributes-router

Usage

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']);
    }

Add controllers

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]);

Define a base URI

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');

Generate a URL from route name

You have the possibility from the name of the route, to generate a URL.

$router->generateUrl('article-comment', ['slug' => 'hello-world', 'id' => 15]);

Contributing

See the Contributing.md file for more information on how to contribute to the project.

About

PHP 8 attributes based router supporting dynamic routes and reverse routing

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages