Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

php - Symfony2 Routing - route subdomains

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://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

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

$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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just to point out that this is now added in 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 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...