dongwei1855 2019-05-07 06:42
浏览 87

Symfony 3.4自定义路由加载器工作,但缓存:清除超时

When following https://symfony.com/doc/3.4/routing/custom_route_loader.html to create a custom route loader, I see my desired route in debug:router but cache:clear takes forever and uses a lot of cpu.

I tried using the "custom service" way and the "custom router" way.

This is the custom route loader:

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;
    }
}

This is my current service definition:

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']]

And the reference in routing.yml

event_routes:
  resource: .
  type: custom_event_routes
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
    • ¥20 matlab yalmip kkt 双层优化问题
    • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
    • ¥88 实在没有想法,需要个思路
    • ¥15 MATLAB报错输入参数太多
    • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
    • ¥15 有赏,i卡绘世画不出
    • ¥15 如何用stata画出文献中常见的安慰剂检验图
    • ¥15 c语言链表结构体数据插入
    • ¥40 使用MATLAB解答线性代数问题