giacomofurlan / slim3annotation-router
Enables to define routes via the @Route(...) annotation
Requires
- php: >=5.6.6
- slim/slim: ^3.0
This package is auto-updated.
Last update: 2025-01-09 14:13:27 UTC
README
This library allows routes to be defined in Slim3 with annotations instead of defining them programmatically (in a similar way Symfony does).
This is free software under the GNU GPL v3. See the licence file for more information.
Supported syntax:
/**
* @Route("/class/route/prefix")
*/
class MyController {
/**
* @Route("/path/{arg}/{arg2}", name="route.name" methods=["GET", "POST"])
*/
public function myAction(ServerRequestInterface $request, ResponseInterface $response, $arg1, $arg2)
{ ... }
}
Rules:
- Controller classes' names MUST end with the suffix "Controller", i.e.
MyTestController
. - Controller constructors MAY ONLY have
App
orContainerInterface
dependencies. @Route
annotation needs at least the route (first argument in quotes)- Classes MAY have the
@Route
annotation specifying only the prefix for the whole class' actions - Methods MAY specify the
@Route
annotation. Optional values are:name
: the name of the route (in order to be called somewhere else)methods
: to specify at which methods the action responds. Default: all
- Methods can specify any argument written in the route (as placeholders) and are mapped per-name, case sensitive.
This means that if there is a route placeholder
{myArgument}
, there MAY be a method's argument$myArgument
. If there is a method's argument called$myArgument
there MUST be a{myArgument}
route placeholder. Exceptions are theServerRequestInterface
,ResponseInterface
andContainerInterface
arguments, that MAY be called with any variable name and are always the standard$request
,$response
and$app->getContainer()
Slim3 objects.
Setup
<?php
use giacomofurlan\slim3AnnotationRouter\Router;
# include your autoload
$app = new \Slim\App(...);
# define variables
new Router($app, $controllersDirectory, $globalRoutePrefix, $cacheDir);
$app->run();
$controllersDirectory
can either be a string, or an array of strings. You can thus define multiple controller
directories, if needed. Subdirectories will be scanned automatically.
$globalRoutePrefix
is optional, default ''
. If set, a global route prefix will be applied to all the parsed routes.
$cacheDir
is optional, default null
. If set, cache files will be generated, so that the second time
the app is loaded it won't have to scan the directories again avoiding reflection classes. Cache is written again every
time a controller file has been modified.