Reputation: 5337
For the project I'm working on I need using dynamic subdomains for every customer that is using the service. By default the / route should match to the indexAction, but every site following the schema slug.domain.com should use the slugIndexAction.
Also, the hostname should be dynamic, too. (Defined in the parameters.yml)
My current setup looks like this:
slug_index:
path: /
host: "{slug}.{domain}"
defaults:
_controller: app.controller.frontend:slugIndexAction
domain: '%domain%'
requirements:
domain: '%domain%'
index:
path: /
defaults:
_controller: app.controller.frontend:indexAction
In this case it always matches the index route, even if I use a subdomain. I also tried using hardcoded slugs and hostnames, but that didn't work either.
When the index route is removed, I get a ResourceNotFoundException / NotFoundHttpException
No route found for "GET /"
http://test.localhost:8000/
Also, would it be possible to use the same controller in both cases as they're basically doing the same, the slugs are used for modifying the css and headings.
Upvotes: 1
Views: 1102
Reputation: 35139
The Symfony docs show an example of routing a sub-domain homepage to a specific action
In a yaml configuration, this is:
projects_homepage:
path: /
host: "{project_name}.example.com"
defaults: { _controller: AppBundle:Main:projectsHomepage }
# $project_name would be a variable to projectsHomepageAction()
homepage:
path: /
defaults: { _controller: AppBundle:Main:homepage }
You can also use the same controller action - I've done similar with different routes (but not on a sub-domain) with default variables, that don't appear in the URL, but are set based on the route that was used:
* # for iframe-use - optionally, with partner-friendly footers
* @Route("/", name="homepage_menus", defaults={"hasMenus"=true, "partnerLinks"=false})
* @Route("/partners", name="homepage_partner_footer", defaults={"hasMenus"=false,"partnerLinks"=true})
*
* @Route("/body-only", name="homepage_body_only", defaults={"hasMenus"=false,"partnerLinks"=false})
*/
public function indexAction(Request $request, $hasMenus = false, $partnerLinks = false)
Upvotes: 3