Skip to content

tarikweiss/slim-attribute-router

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

slim-attribute-router

This is a small library for mapping routes with an attribute.

Requirements

  • PHP 8.1 or higher
  • Slim 4.0 or higher

Installation

composer require tarikweiss/slim-attribute-router

Installation of router

$router = new \Tarikweiss\SlimAttributeRouter\Router(
    ['/path/to/project/src/Action'],
    new \Tarikweiss\SlimAttributeRouter\PublicMethodRouteTargetCreator('run')
);

/** @var \Slim\App $slimApp */
$router->registerRoutes($slimApp);

Installation of Route attribute

The GET and POST is installed on class level, the PATCH method is installed on method level.

#[\Tarikweiss\SlimAttributeRouter\Route(
    methods: ['GET', 'POST'],
    path: '/foo'
)]
class FooAction
{
    /**
     * Here the name given in the constructor of the PublicMethodRouteTargetCreator is used.
     */
    public function run(
        \Psr\Http\Message\ServerRequestInterface $request,
        \Psr\Http\Message\ResponseInterface      $response,
        array                                    $arguments
    ): \Psr\Http\Message\ResponseInterface {
        // Do you request handling and respond to the request.
        // This structure is given by Slim!
        // Have a look at the docs: https://www.slimframework.com/docs/v4/start/installation.html#step-4-hello-world
        return $response;
    }
    
    
    #[\Tarikweiss\SlimAttributeRouter\Route(
        methods: ['PATCH'],
        path: '/foo'
    )]
    public function anotherActionHandler(
        \Psr\Http\Message\ServerRequestInterface $request,
        \Psr\Http\Message\ResponseInterface      $response,
        array                                    $arguments
    ): \Psr\Http\Message\ResponseInterface {
        // Same structure as above, but different scope.
        return $response;
    }
}
}

Custom RouteTargetCreator

If you require to create a custom callable or callable string for Slim's route registration, then you may implement the \Tarikweiss\SlimAttributeRouter\RouteTargetCreator interface. It gives you full handling over the registration level (class or method) and the class (and method) that should be registered.

Here is a sample implementation, the RouteTargetCreator for public methods (shipped with library).

class PublicMethodRouteTargetCreator implements \Tarikweiss\SlimAttributeRouter\RouteTargetCreator
{
    public function __construct(
        public string $classLevelMethodName = 'run'
    )
    {
    }


    public function createRouteTarget(\Tarikweiss\SlimAttributeRouter\RouteLevel $routeLevel, string $class, ?string $method): callable|string
    {
        return match ($routeLevel) {
            \Tarikweiss\SlimAttributeRouter\RouteLevel::LEVEL_CLASS  => $class . ':' . $this->classLevelMethodName,
            \Tarikweiss\SlimAttributeRouter\RouteLevel::LEVEL_METHOD => $class . ':' . $method,
        };
    }
}

About

No description, website, or topics provided.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published