Documentation

AnnotationClassLoader
in package
implements LoaderInterface

AnnotationClassLoader loads routing information from a PHP class and its methods.

You need to define an implementation for the configureRoute() method. Most of the time, this method should define some PHP callable to be called for the route (a controller in MVC speak).

The @Route annotation can be set on the class (for global parameters), and on each method.

The @Route annotation main value is the route path. The annotation also recognizes several parameters: requirements, options, defaults, schemes, methods, host, and name. The name parameter is mandatory. Here is an example of how you should be able to use it: /** * @Route("/Blog") * / class Blog { /** * @Route("/", name="blog_index") * / public function index() { } /** * @Route("/{id}", name="blog_post", requirements = {"id" = "\d+"}) * / public function show() { } }

On PHP 8, the annotation class can be used as an attribute as well: #[Route('/Blog')] class Blog { #[Route('/', name: 'blog_index')] public function index() { } #[Route('/{id}', name: 'blog_post', requirements: ["id" => '\d+'])] public function show() { } }

Tags
author

Fabien Potencier fabien@symfony.com

author

Alexander M. Turek me@derrabus.de

Interfaces, Classes and Traits

LoaderInterface
LoaderInterface is the interface implemented by all loader classes.

Table of Contents

$defaultRouteIndex  : int
$env  : mixed
$reader  : mixed
$routeAnnotationClass  : string
__construct()  : mixed
getResolver()  : LoaderResolverInterface
Gets the loader resolver.
load()  : RouteCollection
Loads from annotations from a class.
setResolver()  : mixed
Sets the loader resolver.
setRouteAnnotationClass()  : mixed
Sets the annotation class to read route properties from.
supports()  : bool
Returns whether this class supports the given resource.
addRoute()  : mixed
configureRoute()  : mixed
createRoute()  : mixed
getDefaultRouteName()  : string
Gets the default route name for a class method.
getGlobals()  : mixed
getAnnotations()  : iteratable<int, Route>
resetGlobals()  : array<string|int, mixed>

Properties

$routeAnnotationClass

protected string $routeAnnotationClass = SymfonyComponentRoutingAnnotationRoute::class

Methods

__construct()

public __construct([Reader|null $reader = null ][, string|null $env = null ]) : mixed
Parameters
$reader : Reader|null = null
$env : string|null = null
Return values
mixed

load()

Loads from annotations from a class.

public load(string $class[, string|null $type = null ]) : RouteCollection
Parameters
$class : string

A class name

$type : string|null = null
Tags
throws
InvalidArgumentException

When route can't be parsed

Return values
RouteCollection

setRouteAnnotationClass()

Sets the annotation class to read route properties from.

public setRouteAnnotationClass(string $class) : mixed
Parameters
$class : string
Return values
mixed

supports()

Returns whether this class supports the given resource.

public supports(mixed $resource[, string|null $type = null ]) : bool
Parameters
$resource : mixed

A resource

$type : string|null = null
Return values
bool

addRoute()

protected addRoute(RouteCollection $collection, Route $annot, array<string|int, mixed> $globals, ReflectionClass $class, ReflectionMethod $method) : mixed
Parameters
$collection : RouteCollection
$annot : Route

or an object that exposes a similar interface

$globals : array<string|int, mixed>
$class : ReflectionClass
$method : ReflectionMethod
Return values
mixed

configureRoute()

protected abstract configureRoute(Route $route, ReflectionClass $class, ReflectionMethod $method, object $annot) : mixed
Parameters
$route : Route
$class : ReflectionClass
$method : ReflectionMethod
$annot : object
Return values
mixed

createRoute()

protected createRoute(string $path, array<string|int, mixed> $defaults, array<string|int, mixed> $requirements, array<string|int, mixed> $options, string|null $host, array<string|int, mixed> $schemes, array<string|int, mixed> $methods, string|null $condition) : mixed
Parameters
$path : string
$defaults : array<string|int, mixed>
$requirements : array<string|int, mixed>
$options : array<string|int, mixed>
$host : string|null
$schemes : array<string|int, mixed>
$methods : array<string|int, mixed>
$condition : string|null
Return values
mixed

getDefaultRouteName()

Gets the default route name for a class method.

protected getDefaultRouteName(ReflectionClass $class, ReflectionMethod $method) : string
Parameters
$class : ReflectionClass
$method : ReflectionMethod
Return values
string

getGlobals()

protected getGlobals(ReflectionClass $class) : mixed
Parameters
$class : ReflectionClass
Return values
mixed

getAnnotations()

private getAnnotations(ReflectionClass|ReflectionMethod $reflection) : iteratable<int, Route>
Parameters
$reflection : ReflectionClass|ReflectionMethod
Return values
iteratable<int, Route>

resetGlobals()

private resetGlobals() : array<string|int, mixed>
Return values
array<string|int, mixed>

Search results