您可以根据域名进行路由:
http://symfony.com/doc/master/components/routing/hostname_pattern.html
有了这个,您可以为移动站点使用不同的控制器,但仍然可以重用您的模型和(至少部分)您的视图。您甚至可以通过在路由中设置参数(基于主机)然后在控制器中使用它来重用一些控制器:
# routing.yml
mobile_homepage:
path: /
host: m.{domain}
defaults: { _controller: AcmeDemoBundle:Main:homepage, mobileVersion: true }
requirements:
domain: %domain%
homepage:
path: /
defaults: { _controller: AcmeDemoBundle:Main:homepage }
然后在你的控制器中:
// Acme/DemoBundle/Controller/MainController.php
public function homepageAction($mobileVersion = false) {
...
if ($mobileVersion) {
// do some mobile things, perhaps return a specific template
} else {
// do some non-mobile things, perhaps return a specific template
}
...
}