【问题标题】:Symfony2 Routing - route subdomainsSymfony2 路由 - 路由子域
【发布时间】:2011-07-18 23:36:41
【问题描述】:

有没有办法在 Symfony2 中设置基于主机名的路由?

我在官方文档中没有找到关于这个主题的任何内容。
http://symfony.com/doc/2.0/book/routing.html

我想根据给定的主机名路由请求:
foo.example.com
bar.example.com
{{subdomain}}.example.com

所以本质上,控制器将获取当前子域作为参数传递。

类似于 Zend 解决方案:
http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.hostname

$hostnameRoute = new Zend_Controller_Router_Route_Hostname(
    ':username.users.example.com',
    array(
        'controller' => 'profile',
        'action'     => 'userinfo'
    )
);
$plainPathRoute = new Zend_Controller_Router_Route_Static('');

$router->addRoute('user', $hostnameRoute->chain($plainPathRoute));

我希望这是可能的,但我只是以某种方式错过了它。
提前致谢!

【问题讨论】:

    标签: php routing symfony


    【解决方案1】:

    Symfony 1.2 有一个plugin,它添加了这个功能。代码在一个文件中只有几百行,移植到 Symfony 2 应该不会太困难。但是 Sensio 的文档还没有。

    您也不能在路由中包含子域并从控制器获取域并在那里处理它。我觉得是这个方法:getHost()

    【讨论】:

    • 所以还没有内置方法。真可惜。
    • 如果在 Sf2 中有一个捆绑包就好了
    【解决方案2】:

    这是我的解决方案:

    在应用程序目录内的config.yml 中添加以下行:

    services:
       kernel.listener.subdomain_listener:
           class: Acme\DemoBundle\Listener\SubdomainListener
           tags:
               - { name: kernel.event_listener, event: kernel.request, method: onDomainParse }
    

    然后创建类SubdomainListener.php为:

    <?php
    
    namespace Acme\DemoBundle\Listener;
    
    use Symfony\Component\EventDispatcher\EventDispatcher;
    use Symfony\Component\EventDispatcher\Event;
    
    class SubdomainListener
    {
       public function onDomainParse(Event $event)
       {
           $request = $event->getRequest();
           $session = $request->getSession();
    
           // todo: parsing subdomain to detect country
    
           $session->set('subdomain', $request->getHost());
       }
    }
    

    【讨论】:

    • 这是一个非常好的方法。谢谢。
    • 我猜如果使用这种方法,缓存会出现问题,每个子域都应该有一个,否则与“bar”路径相同的“foo”页面可能会从“酒吧”,对吗?
    • 我不明白“待办事项”中应该包含的内容。这还不足以获取“子域”的值并在控制器中使用它吗?
    • 更好的是,它可以自动设置用户的语言环境吗?
    【解决方案3】:

    我假设symfony2中的子域路由是根据主机名的子域部分选择定义的控制器的过程,会话变量无助于解析定义的控制器。

    我在内核监听器中设置了请求属性:_controller,像这样

    $request->attributes->set('_controller','AcmeBundle:Demo:main');
    

    这有助于路由到定义的控制器,但我在开发环境中丢失了调试分析器,仍然无法检测到原因

    【讨论】:

      【解决方案4】:

      或者在控制器中获取主机名:

      class DefaultController extends PowmaController {
      
        /**
         * @Route("/test")
         */
        public function testAction() {
          return new Response( 'Hostname ' . $this->getRequestHostnameString() );
        }
      
        function getRequestHostnameString() {
          return $this->getRequest()->getHost();
        }
      

      【讨论】:

        【解决方案5】:

        需要指出的是,它现在已添加到 Symfony v2.2 - http://symfony.com/doc/master/components/routing/hostname_pattern.html

        mobile_homepage:
            path:     /
            host:     m.{domain}
            defaults: { _controller: AcmeDemoBundle:Main:mobileHomepage }
            requirements:
                domain: %domain%
        
        homepage:
            path:  /
            defaults: { _controller: AcmeDemoBundle:Main:homepage }
        

        【讨论】:

        • 要达到顶峰还有很长的路要走,但这里什么都没有! ;)
        【解决方案6】:

        这是一个处理多个域站点的捆绑包:https://github.com/AppVentus/MultiDomainBundle

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-03-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-04-24
          • 2014-02-26
          • 2021-07-01
          相关资源
          最近更新 更多