Implementation for configureRoute() of AnnotationClassLoader

。_饼干妹妹 提交于 2019-12-11 06:11:03

问题


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

回答1:


Ok, the really simple solution (which I found by means of trial and error) is:

$route->setDefault('_controller', $method->class.'::'.$method->name);

This expression gets you something like:

MyPluginBundle\Controller\DefaultController::indexAction

Appanrently, the format MyPluginBundle:Default:index is just a shortcut and not required.

But this still leaves the question open: Is there a simpler (e.g. built-in) way to achieve this?



来源:https://stackoverflow.com/questions/44002811/implementation-for-configureroute-of-annotationclassloader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!