0
votes

Suppose I have two bundles ParentBundle and ChildBundle. ChildBundle "extends" ParentBundle by

// ChildBundle/ChildBundle.php
<?php

namespace ChildBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

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

}

Then, I copied the routing from ParentBundle to ChildBundle, and specify which routing to use in app/config/routing.yml, as well as rename the routing.yml as per Symfony2 bundle inheritance losing parent bundles routes

// app/config/routing.yml
child:
    resource: "@ChildBundle/Resources/config/routing_child.yml"
    hostname_pattern: child.example.com
    prefix:   /

parent:
    resource: "@ParentBundle/Resources/config/routing.yml"
    prefix:   /

After that, I create a template in ChildBundle with same path and name, to override the template in ParentBundle of the same name.

However, it results in loading the template in ChildBundle all the time.

So, my problem is, How do I load the ChildBundle in one domain (i.e. use overriding templates/controllers and such in ChildBundle, when user goes into child.example.com) while use ParentBundle in another domain (i.e. use overriden templates/controllers and such in ParentBundle, when user goes into example.com)?

1

1 Answers

2
votes

You should read this answer I did: Main page to sub applications on Symfony2

In fact you have to create 2 controllers in the web folder, for example: web/app.php, web/app_child.php

Inside app_child.php, call a new environnement, here called "child":

// ...    
$kernel = new AppKernel('child', false);
// ...

Create a config_child.yml that will be specific to child bundle, you can paste here the config.yml content or even import config.yml to prevent duplicate code:

// config_child.yml
imports:
    - { resource: config.yml }

Create a new routing file that includes the routes of child bundle, called routing_child.yml for example, and import this file in config_child.php:

framework:
    router:
        resource: "%kernel.root_dir%/config/routing_child.yml"

Remove the child bundle routes from your classic routing.yml file.

Now play with your web/.htaccess to call the right environnement depending on the subdomain:

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

That's it, now your applciation will load the right routing config depending on the domain ;)