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)?
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”:
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:
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:
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:
That’s it, now your applciation will load the right routing config depending on the domain 😉