Symfony 3.4自定义路由加载器可以运行,但是cache:clear超时

时间:2019-05-07 06:42:24

标签: php symfony

跟随https://symfony.com/doc/3.4/routing/custom_route_loader.html创建自定义路由加载器时,我在debug:router中看到了我想要的路由,但是cache:clear占用了很多时间,并且占用了大量的CPU。

我尝试使用“自定义服务”方式和“自定义路由器”方式。

这是自定义路由加载器:

use Symfony\Component\Config\Loader\Loader;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Doctrine\ORM\EntityManagerInterface;

class EventRouteLoader extends Loader  {

    private $isLoaded = false;
    protected $em;

    public function init(EntityManagerInterface $em) {
        $this->em = $em;
    }

    public function load($resource, $type = NULL) {

        if (true === $this->isLoaded) {
            throw new \RuntimeException('Do not add the "extra" loader twice');
        }

        $routes = new RouteCollection();

        foreach ($this->em->getRepository('AppBundle:Event')->findAll() as $event) {
            if ($event->getAuthType() == 'with_code' && $event->getAccessCode() && strlen($event->getAccessCode()) > 4) {
                $path = '/' . $event->getAccessCode();
                $defaults = [
                    '_controller' => 'AppBundle:Invitation:page',
                    'event' => $event,
                    'code' => $event->getAccessCode(),
                    'activePageNumber' => 1,
                ];
                $requirements = [];
                $route = new Route($path, $defaults, $requirements);

                // add the new route to the route collection
                $routeName = 'eventRoute'.$event->getId();
                $routes->add($routeName, $route);
            }
        }

        $this->isLoaded = true;

        return $routes;
    }

    public function supports($resource, $type = null){
        return 'custom_event_routes' === $type;
    }
}

这是我当前的服务定义:

em:
  class: Doctrine\ORM\EntityManager
  factory: ['@doctrine', 'getManager']
event_route_loader:
  class: AppBundle\Routing\EventRouteLoader
  autowire: false
  autoconfigure: false
  public: true
  tags: [routing.loader]
  calls:
    - [ init, ['@em']]

以及routing.yml中的引用

event_routes:
  resource: .
  type: custom_event_routes

0 个答案:

没有答案