24

Is there a way to set up hostname based routing in Symfony2?

I didn't find anything about this topic in the official documentation.
http://symfony.com/doc/2.0/book/routing.html

I want to route the request based on the given hostname:
foo.example.com
bar.example.com
{{subdomain}}.example.com

So in essence, the controller would get the current subdomain passed as a parameter.

Similar to the Zend solution:
http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.hostname

$hostnameRoute = new Zend_Controller_Router_Route_Hostname( ':username.users.example.com', array( 'controller' => 'profile', 'action' => 'userinfo' ) ); $plainPathRoute = new Zend_Controller_Router_Route_Static(''); $router->addRoute('user', $hostnameRoute->chain($plainPathRoute)); 

I hope that it's possible and I just missed it somehow.
Thanks in advance!

6 Answers 6

43

Just to point out that this is now added in Symfony v2.2 - http://symfony.com/doc/master/components/routing/hostname_pattern.html.

mobile_homepage: path: / host: m.{domain} defaults: { _controller: AcmeDemoBundle:Main:mobileHomepage } requirements: domain: %domain% homepage: path: / defaults: { _controller: AcmeDemoBundle:Main:homepage } 
Sign up to request clarification or add additional context in comments.

1 Comment

Long ways to go to the top, but here's nothing! ;)
25

This is my solution:

In the config.yml inside app dir add the following lines:

services: kernel.listener.subdomain_listener: class: Acme\DemoBundle\Listener\SubdomainListener tags: - { name: kernel.event_listener, event: kernel.request, method: onDomainParse } 

Then create the class SubdomainListener.php as:

<?php namespace Acme\DemoBundle\Listener; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\Event; class SubdomainListener { public function onDomainParse(Event $event) { $request = $event->getRequest(); $session = $request->getSession(); // todo: parsing subdomain to detect country $session->set('subdomain', $request->getHost()); } } 

5 Comments

I guess if this method is used there will be problems with the cache, there should really be one per subdomain otherwise pages for 'foo' with the same path as 'bar' might return a cached page from 'bar', am I right?
I don't get what's supposed to be in "todo". Isn't this enough to get the value of "subdomain" and use it in the controller ?
Even better, could it automaticly set the user's locale ?
Note that this just sets a session variable with the subdomain, it doesn't do any routing. When you're using the subdomain for language or users this is fine, but not for true multi-site.
2

Alternatively get hostname in the controller:

class DefaultController extends PowmaController { /** * @Route("/test") */ public function testAction() { return new Response( 'Hostname ' . $this->getRequestHostnameString() ); } function getRequestHostnameString() { return $this->getRequest()->getHost(); } 

Comments

2

I assume that subdomain routing in symfony2 is process of choose defined controller in according to subdomain part of hostname, and session variable is not help to resolve defined controller.

I set request attribute: _controller, in kernel listener like this

$request->attributes->set('_controller','AcmeBundle:Demo:main'); 

This is help to route to defined controller, but I lose debug profiler in dev environment, still I can not detect a cause

Comments

1

There is a plugin for Symfony 1.2 that adds this functionality. The code is only a few hundred lines in a single file and shouldn't be too diffcult to port to Symfony 2. But the documentation from Sensio isn't quite there yet.

You could also not include the subdomain in the route and fetch the domain from the controller and process it there. I think it's this method: getHost()

2 Comments

So no built-in method yet. That's a pity.
That would be nice to have a bundle for that in Sf2
0

Here is a bundle that handle multiple domain site : https://github.com/AppVentus/MultiDomainBundle

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.