Symfony2 Routing - route subdomains

后端 未结 6 990
半阙折子戏
半阙折子戏 2020-11-29 19:13

Is there a way to set up hostname based routing in Symfony2?

I didn\'t find anything about this topic in the official documentation.
http://sy

6条回答
  •  半阙折子戏
    2020-11-29 20:10

    This is my solution:

    In the config.yml inside app dir add the following lines:

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

    Then create the class SubdomainListener.php as:

    getRequest();
           $session = $request->getSession();
    
           // todo: parsing subdomain to detect country
    
           $session->set('subdomain', $request->getHost());
       }
    }
    

提交回复
热议问题