kryptamine/groot

PHP 8 attributes based router adapter

0.0.1 2021-09-26 12:15 UTC

This package is auto-updated.

Last update: 2024-04-26 18:16:14 UTC


README

This package is an adapter that provides the ability to integrate PHP 8 attributes with any router you'd prefer to use.

use Kryptamine\Groot\Attributes\Get;
use Kryptamine\Groot\Attributes\Post;
use Kryptamine\Groot\Attributes\Controller;

#[Controller('users')]
class UserController
{
    #[Get('/')]
    public function getUsers(): void
    {

    }
    
    #[Get('/{id:\d+}')]
    public function getUser(int $id): void
    {

    }
    
    #[Post('/')]
    public function saveUser(): void
    {

    }
    
    ...
}

Installation

  1. Install the package via composer:
composer require kryptamine/groot
  1. Use a supported adapter or implement your own

Usage example

namespace Kryptamine\Groot;

use Kryptamine\Groot\Router;
use Bramus\Router\Router as BramusRouter;
use Kryptamine\Groot\Adapters\BramusRouterAdapter;

require 'vendor/autoload.php';

$bramusRouter = new BramusRouter();

$router = new Router(BramusRouterAdapter::fromBramusRouter($bramusRouter), [
    UserController::class,
]);

$router->register();

$bramusRouter->run();

Supported Adapters

bramus/router

nikic/FastRoute

To create your own adapter, you should implement RouteRegistrarAdapterInterface