Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
选择父/子捆绑包取决于Symfony2中的域_Symfony_Symfony 2.2 - Fatal编程技术网

选择父/子捆绑包取决于Symfony2中的域

选择父/子捆绑包取决于Symfony2中的域,symfony,symfony-2.2,Symfony,Symfony 2.2,假设我有两个bundleParentBundle和ChildBundleChildBundle“扩展”ParentBundleby // ChildBundle/ChildBundle.php <?php namespace ChildBundle; use Symfony\Component\HttpKernel\Bundle\Bundle; class ChildBundle extends Bundle { public function getParent()

假设我有两个bundle
ParentBundle
ChildBundle
ChildBundle
“扩展”
ParentBundle
by

// ChildBundle/ChildBundle.php
<?php

namespace ChildBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class ChildBundle extends Bundle
{
    public function getParent()
    {
        return 'ParentBundle';
    }

}
之后,我在
ChildBundle
中创建了一个具有相同路径和名称的模板,以覆盖同名的
ParentBundle
中的模板

但是,它会导致始终在
ChildBundle
中加载模板


因此,我的问题是,如何在一个域中加载
ChildBundle
(即,当用户进入child.example.com时,在
ChildBundle
中使用覆盖模板/控制器等),而在另一个域中使用
ParentBundle
(即,当用户进入example.com时,使用覆盖的模板/控制器以及
ParentBundle
中的类似内容)

您应该阅读我的回答:

事实上,您必须在web文件夹中创建2个控制器,例如: web/app.php,web/app_child.php

在app_child.php中,调用一个新环境,这里称为“child”:

创建一个特定于子捆绑包的config_child.yml,您可以将config.yml内容粘贴到此处,甚至可以导入config.yml以防止重复代码:

// config_child.yml
imports:
    - { resource: config.yml }
创建一个包含子包路由的新路由文件,例如称为routing_child.yml,并在config_child.php中导入此文件:

framework:
    router:
        resource: "%kernel.root_dir%/config/routing_child.yml"
从classic routing.yml文件中删除子捆绑路由

现在使用web/.htaccess调用正确的环境,具体取决于子域:

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Hit child app
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{HTTP_HOST} !^child\.example.com$ [NC]
    RewriteRule ^(.*)$ app_child.php [QSA,L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>

重新启动发动机
#点击儿童应用程序
重写cond%{REQUEST_FILENAME}!-f
RewriteCond%{HTTP_HOST}!^child\.example.com$[NC]
重写规则^(.*)$app_child.php[QSA,L]
重写cond%{REQUEST_FILENAME}!-f
重写规则^(.*)$app.php[QSA,L]

就是这样,现在您的应用程序将根据域加载正确的路由配置;)

我的同事认为它太粗糙,转而扩展控制器/模板。无论如何,谢谢你的主意。
framework:
    router:
        resource: "%kernel.root_dir%/config/routing_child.yml"
<IfModule mod_rewrite.c>
    RewriteEngine On

    # Hit child app
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{HTTP_HOST} !^child\.example.com$ [NC]
    RewriteRule ^(.*)$ app_child.php [QSA,L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>