php Symfony2 路由 - 路由子域
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5366234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Symfony2 Routing - route subdomains
提问by user523736
Is there a way to set up hostname based routing in Symfony2?
有没有办法在Symfony2 中设置基于主机名的路由?
I didn't find anything about this topic in the official documentation.
http://symfony.com/doc/2.0/book/routing.html
我在官方文档中没有找到有关此主题的任何信息。
http://symfony.com/doc/2.0/book/routing.html
I want to route the request based on the given hostname:
foo.example.com
bar.example.com
{{subdomain}}.example.com
我想根据给定的主机名路由请求:
foo.example.com
bar.example.com
{{subdomain}}.example.com
So in essence, the controller would get the current subdomain passed as a parameter.
所以本质上,控制器将获取作为参数传递的当前子域。
Similar to the Zend solution:
http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.hostname
类似于 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));
I hope that it's possible and I just missed it somehow.
Thanks in advance!
我希望这是可能的,我只是以某种方式错过了它。
提前致谢!
回答by Dan Blows
Just to point out that this is now added in Symfony v2.2 - http://symfony.com/doc/master/components/routing/hostname_pattern.html.
只是要指出,这现在已添加到 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 }
回答by Alexander Vasilenko
This is my solution:
这是我的解决方案:
In the config.yml
inside app dir add the following lines:
在config.yml
内部应用程序目录中添加以下行:
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:
然后将类创建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());
}
}
回答by Aleksandr N. Ryzhov
I assume that subdomain routing in symfony2 is process of choose defined controller in according to subdomain part of hostname, and session variable is not help to resolve defined controller.
我假设symfony2中的子域路由是根据主机名的子域部分选择定义的控制器的过程,会话变量无助于解析定义的控制器。
I set request attribute: _controller, in kernel listener like this
我设置了请求属性:_controller,在内核监听器中是这样的
$request->attributes->set('_controller','AcmeBundle:Demo:main');
This is help to route to defined controller, but I lose debug profiler in dev environment, still I can not detect a cause
这有助于路由到定义的控制器,但我在开发环境中丢失了调试分析器,但我仍然无法检测到原因
回答by Mike
Alternatively get hostname in the controller:
或者在控制器中获取主机名:
class DefaultController extends PowmaController {
/**
* @Route("/test")
*/
public function testAction() {
return new Response( 'Hostname ' . $this->getRequestHostnameString() );
}
function getRequestHostnameString() {
return $this->getRequest()->getHost();
}
回答by Josh David Miller
There is a pluginfor Symfony 1.2 that adds this functionality. The code is only a few hundred lines in a single file and shouldn't be too diffcult to port to Symfony 2. But the documentation from Sensio isn't quite there yet.
Symfony 1.2有一个插件添加了这个功能。代码在一个文件中只有几百行,移植到 Symfony 2 应该不会太困难。但是 Sensio 的文档还没有完全到位。
You could also not include the subdomain in the route and fetch the domain from the controller and process it there. I think it's this method: getHost()
您也不能在路由中包含子域并从控制器获取域并在那里处理它。我认为是这个方法:getHost()
回答by Paul Andrieux
Here is a bundle that handle multiple domain site : https://github.com/AppVentus/MultiDomainBundle
这是一个处理多个域站点的包:https: //github.com/AppVentus/MultiDomainBundle