Teshan N.
Teshan N.

Reputation: 2535

Symfony match a route to host is not working

I need to set a route in my Symfony (2.8) project to be loaded when one of the sub domains are accessed. Let's say my site is hosted under "example.com" and its admin needs to be loaded for "admin.example.com". I know in Symfony we can use host: admin.example.com in the routing file (as instructed in http://symfony.com/doc/2.8/routing/hostname_pattern.html). But when I add it in any of the routing.yml files (app/config/routing.yml and AdminBundle/Resources/config/routing.yml) it does not work.

I get the error

No route found for "GET /admin/"

How can I load a different bundle when a sub domain is called? What am I doing wrong here?

app/config/routing.yml

app:
    resource: '@AppBundle/Resources/config/routing.yml'
    prefix:   /

admin:
    resource: "@AdminBundle/Resources/config/routing.yml"
    prefix:   /admin/

AdminBundle/Resources/config/routing.yml

admin_homepage:
    path:     /
    host:     admin.example.com
    defaults: { _controller: AdminBundle:Index:home }

Upvotes: 0

Views: 917

Answers (1)

MaZaN
MaZaN

Reputation: 117

I think you should swap host and prefix positions like this:

    app:
        resource: '@AppBundle/Resources/config/routing.yml'
        prefix:   /

    admin:
        resource: "@AdminBundle/Resources/config/routing.yml"
        host:     admin.example.com
    admin_homepage:
        path:     /admin
        defaults: { _controller: AdminBundle:Index:home }

Upvotes: 2

Related Questions