简体   繁体   中英

Symfony 3 - routing depending on the host name

I'm trying to conditionally use routes with multipe domians.

What I have currently is:

admin_routes:
    host:     "admin.{domain}"
    resource: "@AppBundle/Controller/Admin"
    type: annotation
    requirements:
        domain: domain.local|domain2.local
    defaults: { domain: domain.local }

user_routes:
    host:     "user.{domain}"
    resource: "@AppBundle/Controller/User"
    type: annotation
    requirements:
        domain: domain3.local|domain4.local
    defaults: { domain: domain3.local }

Controllers in both folders have same route names as this is how the app is built. The above is not working as the app is still loading both routes. For user domain urls from admin are built. Is what I'm trying to achieve possible at all or am I doing this wrong way completely?

Ultimatelly what I'd like is to set of controllers is only "visible" for given hostname.

The routes need to have unique name. If there are 2 routes with same name the one will override another. As for host specification, this only affects if the route is matched or not. Similar to route requirements.

Solution to your problem is to name routes in both Controllers differently. One thing how to achieve this is by route name prefixes in Controller class annotation.

/**
 * @Route("/path", name="admin_")
 */

and

/**
 * @Route("/path", name="user_")
 */

https://symfony.com/blog/new-in-symfony-3-4-prefix-all-controller-route-names

Routes can have same /path prefix but their name needs to differ. This way they can match same path with different conditions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM