1
votes

here is the context of the question: I want to build a multi-site installation of Symfony2. Having on one side the admin and the content types which are common for every site but a differenciation of the templates for each site

Hence, I would need routing specific per site visited. Let's say we have 2 sites sub1.site.com & sub2.site.com, for the same pattern '/' I would like to be able to route differently the request for each subsite.

1st solution tested: use the 'host' of the routing system as in here (Host routing in Symfony). Implemented like that it didn't worked

routingFirstSite:
    resource: "@BundleName/Resources/config/routing1.yml"
    host: "sub1.site.com"
    prefix: /

routingSecondSite:
    resource: "@BundleName/Resources/config/routing2.yml"
    host: "sub2.site.com"
    prefix: /                             ==>*(added after 1st comment)

==> This solution doesn't work, it loads the second regarless of the host used...

2nd solution put in place : use a dynamic routing upon listening to the kernel.request event as in the following solution : Symfony custom/dynamic router.

It works perfectly. I then put included directly the routing of the admin into the routing.yml file and have the "sites" routing loaded up dynamically.

BUT

upon the submit of any form to create/edit/delete any content in the site, the admin controller have the following return which is giving an error 500 (can't find the route 'index')

return $this->redirect($this->generateUrl('index'));

(needless to say that the 'index' route is defined in all of the sub_routing.yml files for all sites).

to understand what was in the request to be sure that I was properly arriving, I modified to the following:

var_dump($this->getRequest()->attributes->all());
return $this->redirect($this->generateUrl('index'));

And by miracle this is working, I'm redirected to the index of the site!

My question is then : What's the mechanism at stake here? Why is it not working in the first place and well in the second? What should I do to have it working in the first situation?

thanks a lot for your time,

1
your first solution should work fine, maybe it´s because you missed the prefix in routingSecondSitejohn Smith
Hi, thanks but it's an error of copy-paste as the prefix argument existed in my routing.yml file... (I've updated the question)Yannick

1 Answers

0
votes

in my project i always added a requirement to match host like this :

and i use a variable and a parameter in parameters.yml eg %your_host%

routingFirstSite:
    resource: "@BundleName/Resources/config/routing1.yml"
    prefix:   /
    host: "{your_host}"
    requirements:
        your_host: %your_host%


routingFirstSite:
    resource: "@BundleName/Resources/config/routing2.yml"
    prefix:   /
    host: "{your_second_host}"
    requirements:
        your_second_host: %your_second_host%

and in parameters.yml you have like:

your_host: sub1.site.com
your_second_host: sub2.site.com

without quotes you should give it a try, hould work, if not pls tell which symfony2 version youre running