Symfony2 中的动态路由前缀


Dynamic route prefix in Symfony2

我正在创建一个SaaS,symfony2提供私人网站。我想做的是让人们以这种方式访问网站:

http://www.mydomain.com/w/{website_name}

这是我正在使用的路由配置:

websites:
    resource: "@MyBundle/Resources/config/routing.yml"
    prefix:   /w/{website_name}

问题是,例如,当我尝试访问时 http://www.mydomain.com/w/chucknorris 出现错误:

在呈现模板期间引发异常("一些 缺少生成 URL 的必需参数("website_name") 对于路线"websites_homepage"。在 "MyBundle:Publication:publicationsList.html.twig"。

我的理解是我的路由配置运行良好,但是当我调用路由器在网站中生成url时,它不知道"上下文"{website_name} url参数。

我想象的一个解决方案是找到一种方法,在上下文中设置此参数时自动无缝地注入此参数。

到目前为止,我所能做的就是创建一个服务来以这种方式获取此参数:

public function __construct(Registry $doctrine, ContainerInterface $container) {
        $website_name = $container->get('request')->get("website_name");
        if (!empty($website_name)) {
            $repository = $doctrine->getManager()->getRepository('MyBundle:website');
            $website = $repository->findOneByDomain($website_name);
            if ($website) {
                $this->website = $website;
            } else {
                throw new 'Symfony'Component'HttpKernel'Exception'NotFoundHttpException();
            }
        } else {
            $this->isPortal = true;
        }
}

我的问题是:如何将该参数注入到生成的所有 url 中,以避免出现参数丢失的错误,并且每次在控制器或树枝中调用路由器时都不必手动指定它?(我想这是关于请求事件的事情,但我不知道如何做到这一点,特别是如何根据 symfony2 的良好用法来做到这一点)

更新这是我基于symfony提供的locallistener创建的监听器:

    <?php    
class WebsiteNameRouteEventListener implements EventSubscriberInterface {    
        private $router;    
        public function __construct(RequestContextAwareInterface $router = null) {
            $this->router = $router;
        }
        public function onKernelResponse(FilterResponseEvent $event) {            
            $request = $event->getRequest();
            $this->setWebsiteName($request);
        }    
        public function onKernelRequest(GetResponseEvent $event) {
            $request = $event->getRequest();    
            $this->setWebsiteName($request);           
        }    
        public static function getSubscribedEvents() {
            return array(
                // must be registered after the Router to have access to the _locale
                KernelEvents::REQUEST => array(array('onKernelRequest', 16)),
                KernelEvents::RESPONSE => 'onKernelResponse',
            );
        }    
        private function setWebsiteName(Request $request) {           
            if (null !== $this->router) {
                echo "NEW CODE IN ACTION";die();
                $this->router->getContext()->setParameter('website_name', $request->attributes->get("website_name"));
            }
        }    
    }

但是我仍然收到此错误:

在呈现模板期间引发异常("一些 缺少生成 URL 的必需参数("website_name") 为路线"主页"。在 "MyBundle:Publication:publicationsList.html.twig"。500 内部 服务器错误 - Twig_Error_Runtime 1 个链接异常:

MissingMandatoryParametersException »

没有我的回声"....";die() 正在执行,所以我想 twig 在执行路径(路由名称)代码时没有触发我正在侦听的事件。

知道吗?

您可以使用路由器上下文。

$this->router->getContext()->setParameter('website_name', $website_name);

请参阅此文件:https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpKernel/EventListener/LocaleListener.php