Sunil Kumar
Sunil Kumar

Reputation: 1381

Generate appropriate urls in multiple sub-domain hostname in Symfony2

I am developing a multi sub-domain web application in Symfony 2.

Subdomains:

As mentioned in the documentation here, I configured routing as below :

Parent Routing for member.example.com:

my_app_member:
    resource: "@member.yml"
    prefix:   /
    host:     "member.example.com"

Note : The above routing is the parent route config for all routes for member.example.com defined in member.yml.

**Now I have anoter route for admin.example.com : **

admin_user_mod:
    path:     /admin/new
    defaults: { _controller: "somecontroller" }

However if I have to generate a full url for the route admin_user_mod using code :

$modLink = $this->get("router")->generate('admin_user_mod');

The generated route path is correct but the base url still stays as member.example.com which should be admin.example.com

Is there an way, or I am missing anything in above route configuration to get the desired results.

OR

Is there any symfony event listener to overwrite router's "generate()" method call?

All your inputs are highly appreciable.

Thank you

Upvotes: 1

Views: 4336

Answers (2)

Trappar
Trappar

Reputation: 231

As it says in the Routing Documentation, passing true as the third parameter for the generate method will generate an absolute URL.

So in your case it would be...

$modLink = $this->get('router')->generate('admin_user_mod', [], true);

Just make sure you specify the host in the route configuration when doing this. So for example your route should be...

admin_user_mod:
    path: /admin/new
    host: admin.example.com
    defaults: { _controller: "somecontroller" }

Upvotes: 0

Konstantin Pereiaslov
Konstantin Pereiaslov

Reputation: 1814

Router has getContext() method you can use:

$context = $this->getContainer()->get('router')->getContext();
$context->setHost('admin.example.com');

Upvotes: 5

Related Questions