In my Symfony 3.2 app, I create a custom router that extends from Symfony\Bundle\FrameworkBundle\Routing\Router
.
As required, it implements a getRouteCollection()
method:
public function getRouteCollection() {
$locator = new FileLocator(__DIR__ . '/../Controller');
$annotationReader = new Doctrine\Common\Annotations\AnnotationReader();
$classLoader = new AnnotationClassLoader($annotationReader);
$routeLoader = new Symfony\Component\Routing\Loader\AnnotationDirectoryLoader($locator, $classLoader);
$routes = $routeLoader->load('.\\', 'annotation');
return $routes;
}
The AnnotationClassLoader
that is used here is my own, it extends the abstract Symfony\Component\Routing\Loader\AnnotationClassLoader
class. It implements only one method:
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot) {
$route->setDefault('_controller', '???');
return $route;
}
I want to use this to set the default controller for a route that was found in the annotations. The AnnotationReader
reads all the routes from the source code perfectly fine, but it doesn't set their default controllers at all, which is what I am trying to do in configureRoute
. That function seems to be called on every route that was found.
The problem now is to find the appropriate expression to replace '???'
with. In the end, I need an outcome such as 'MyPluginBundle:ControllerName:actionName'
. That means I either need to build that string with the information I have (which are the four arguments of the function) or I am taking a totally wrong approach and there is a much easier way to do this.
Example dump of $route
:
As you can see, there is no default for _controller
yet.
object(Symfony\Component\Routing\Route)
private 'path' => string '/my/route' (length=9)
private 'host' => string '' (length=0)
private 'schemes' =>
array (size=0)
empty
private 'methods' =>
array (size=0)
empty
private 'defaults' =>
array (size=0)
empty
private 'requirements' =>
array (size=0)
empty
private 'options' =>
array (size=1)
'compiler_class' => string 'Symfony\Component\Routing\RouteCompiler' (length=39)
private 'compiled' => null
private 'condition' => string '' (length=0)
Example dump of $class
:
object(ReflectionClass)
public 'name' => string 'MyPluginBundle\Controller\DefaultController' (length=43)
Example dump of $method
:
object(ReflectionMethod)[174]
public 'name' => string 'indexAction' (length=11)
public 'class' => string 'MyPluginBundle\Controller\DefaultController' (length=43)
Example dump of $annot
:
object(Sensio\Bundle\FrameworkExtraBundle\Configuration\Route)
protected 'service' => null
private 'path' (Symfony\Component\Routing\Annotation\Route) => string '/my/route' (length=9)
private 'name' (Symfony\Component\Routing\Annotation\Route) => string 'name_of_the_route' (length=17)
private 'requirements' (Symfony\Component\Routing\Annotation\Route) =>
array (size=0)
empty
private 'options' (Symfony\Component\Routing\Annotation\Route) =>
array (size=0)
empty
private 'defaults' (Symfony\Component\Routing\Annotation\Route) =>
array (size=0)
empty
private 'host' (Symfony\Component\Routing\Annotation\Route) => null
private 'methods' (Symfony\Component\Routing\Annotation\Route) =>
array (size=0)
empty
private 'schemes' (Symfony\Component\Routing\Annotation\Route) =>
array (size=0)
empty
private 'condition' (Symfony\Component\Routing\Annotation\Route) => null