【问题标题】:Symfony2 bundles with same routing具有相同路由的 Symfony2 捆绑包
【发布时间】:2012-07-14 02:14:12
【问题描述】:

我在 sf2 中有两个包,每个包都有相同的路由。示例:/博客

捆绑A

user_blog:
    pattern:  /blog
    defaults: { _controller: UserBlogBundle:Default:blog }

捆绑 B

team_blog:
    pattern:  /blog
    defaults: { _controller: TeamBlogBundle:Default:blog }

通过会话参数,我会知道哪个捆绑包将运行请求

对不起,

我想根据会话参数运行正确的包

例子

{{
if($Parameter ='A')
 run user_blog
else
 run team_blog
}}

我做这个:

//src/project/TestBundle
use Symfony\Component\Routing\RouteCollection;

$collection = new RouteCollection();
$req = $this->getRequest();
if($req->server->get('SERVER_NAME') == 'www.domainA.com')
$collection->addCollection($loader->import("@BundleABundle/Resources/config/routing.php"));
else
$collection->addCollection($loader->import("@BundleBBundle/Resources/config/routing.php"));
return $collection;

这是平常的事吗?

【问题讨论】:

  • 你试过你的代码了吗?你有什么错误吗?
  • 还没试过,想做最好的,我正在寻找一种更“漂亮”的方式来做到这一点

标签: symfony routing bundle


【解决方案1】:

在我看来,每条路线都必须是独一无二的。您应该牢记这一点来构建您的应用程序。否则,我认为您在访问这些路由时可能会出现一些随机行为。

【讨论】:

    【解决方案2】:

    在2.2版本参数主机上解决了

    http://symfony.com/doc/master/components/routing/hostname_pattern.html

    Bundle A
    
    user_blog:
        pattern:  /blog
        host: a.example.com
        defaults: { _controller: UserBlogBundle:Default:blog }
    
    Bundle B
    
    team_blog:
        pattern:  /blog
        host: b.example.com
        defaults: { _controller: TeamBlogBundle:Default:blog }
    

    【讨论】:

      猜你喜欢
      • 2016-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-26
      • 2012-12-09
      相关资源
      最近更新 更多