【问题标题】:Symfony Match Route with Dynamic Default SubdomainSymfony 匹配路由与动态默认子域
【发布时间】:2014-10-03 15:30:04
【问题描述】:

我知道可以匹配多个子域上的路由,如下所示:

irc_backend.report.stacking_issue:
    path: /reports/stacking-issues
    host: {subdomain}.domain.com
    defaults:  
        _controller: IRCBackendBundle:Reports/Product/StackingIssueReport:index
        subdomain: backend
    requirements:
        subdomain: backend|dev.backend

这可行,但这种方法的问题是每次我使用路由器生成 URL 时,我都必须指定要使用的子域。我希望避免每次从路由器获取 url 时都必须传入当前子域,而是默认使用与当前请求相同的子域。我需要一种动态设置默认子域值的方法:

irc_backend.report.stacking_issue:
    path: /reports/stacking-issues
    host: {subdomain}.domain.com
    defaults:  
        _controller: IRCBackendBundle:Reports/Product/StackingIssueReport:index
        subdomain: %subdomain%
    requirements:
        subdomain: backend|dev.backend

我尝试使用事件侦听器设置容器参数,但在调用侦听器时,容器已经编译,因此您会收到“无法在冻结的 ParameterBag 上调用 set()”。如果您尝试在那里设置容器参数。

【问题讨论】:

  • 对我来说最简单的方法是创建新路线:symfony.com/doc/2.0/components/routing/introduction.html.
  • 但是在使用路由器生成 URL 时,如何决定使用哪个路由名称呢?
  • 我的错,路由器中没有任何东西可以处理主机。您可以与您的听众和相关课程一起编辑您的第一篇文章吗?

标签: symfony


【解决方案1】:

不幸的是,Symfony 本身还没有内置功能来从当前路由中回收丢失的路由参数 - 我认为这需要通过越来越多的 SaaS 解决方案来解决,至少对于主机而言驻留路由参数。我通过简单地扩展 Symfony\Bridge\Twig\Extension\RoutingExtension 解决了完全相同的问题,如果没有将域参数提供给视图助手,则提供当前相应的路由参数。

我在 AppBundle\Extension\Twig 中创建了一个 DomainAwareRoutingExtension 类,如下所示

<?php

namespace AppBundle\Extension\Twig;

use Symfony\Bridge\Twig\Extension\RoutingExtension;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class DomainAwareRoutingExtension extends RoutingExtension
{

    /** @var \Symfony\Component\HttpFoundation\Request */
    private $request;

    /**
     * DomainAwareRoutingExtension constructor.
     *
     * @param UrlGeneratorInterface  $generator
     * @param null|RequestStack      $requestStack
     */
    public function __construct(UrlGeneratorInterface $generator, RequestStack $requestStack = null)
    {
        parent::__construct($generator);

        $this->request = ($requestStack) ? $requestStack->getCurrentRequest() : null;
    }

    public function getPath($name, $parameters = [], $relative = false)
    {
        if (empty($parameters['domain']) && !empty($this->request) && !empty($this->request->attributes->get('domain'))) {
            $parameters['domain'] = $this->request->attributes->get('domain');
        }
        return parent::getPath($name, $parameters, $relative);
    }

}

并将其注册为服务

#services.yml
parameters:
twig.extension.routing.class: AppBundle\Extension\Twig\DomainAwareRoutingExtension

services:
    twig.extension.routing:
        class: '%twig.extension.routing.class%'
        public: false
        arguments:
            - '@router'
            - '@request_stack'

在您的情况下,您应该将代码中的“域”替换为“子域”。感谢@Cerad 和@Gambit 为Symfony2 Twig overriding default path function 提供灵感。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-29
    • 2021-07-01
    • 1970-01-01
    • 2016-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多