Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't support multiple host routing #2177

Open
BenoitDuffez opened this issue Jun 16, 2023 · 2 comments
Open

Doesn't support multiple host routing #2177

BenoitDuffez opened this issue Jun 16, 2023 · 2 comments

Comments

@BenoitDuffez
Copy link
Contributor

I have a single Symfony app that is served from 3 different domains and presents a different app/UI on each.
Say it's app.domain.com, www.domain.com and api.domain.com.

Here is the configuration

config/
├── bootstrap.php
├── bundles.php
├── packages
...
├── routes
│   ├── app.yaml
│   ├── landing.yaml
│   └── api.yaml
├── routes.yaml
└── services.yaml

Then config/routes/app.yaml has the following (similar with config/routes/landing.yaml and config/routes/api.yaml):

controllers:
  resource:
    path: ../../src/Controller/App      # this will have Landing and Api instead of App in the other files
    namespace: Company\Controller\App
  type: attribute
  name_prefix: app.                     # this will be landing. and api. in the other files
  host:
    - app.domain.com                    # this will also different in the other files
    - staging.app.domain.com
    - tests.app.domain.local
    - app.localhost

This creates several routes and Symfony apparently assumes it's the locale (it adds it as a .X suffix):

$ ./bin/console debug:router | awk '/homepage/||NR<4'
 --------------------------- ---------- -------- --------------------------- ---------
  Name                        Method     Scheme   Host                        Path
 --------------------------- ---------- -------- --------------------------- ---------
  app.homepage.0              ANY        ANY      app.domain.com              /
  app.homepage.1              ANY        ANY      staging.app.domain.com      /
  app.homepage.2              ANY        ANY      tests.app.domain.local      /
  app.homepage.3              ANY        ANY      app.localhost               /
  landing.homepage.0          ANY        ANY      www.domain.com              /
  landing.homepage.1          ANY        ANY      staging.domain.com          /
  landing.homepage.2          ANY        ANY      tests.domain.local          /
  landing.homepage.3          ANY        ANY      landing.localhost           /

Now in the IDE the routes are referenced to as {{ path('app.homepage') }} for example, but the plugin underlines saying the route doesn't exist and that I should pick among app.homepage.0, app.homepage.1, app.homepage.2, app.homepage.3.

image
image

The url_{generating,matching}_routes.php files both contains the routes similar to what is shown above with the debug:router invocation.
It contains an additional setting: the locale:

    'app.homepage.0' => [[], ['_controller' => 'Company\\Controller\\App\\HomepageController::index', '_locale' => 0, '_canonical_route' => 'app.homepage'], [], [['text', '/']], [['text', 'app.domain.com']], [], []],
    'app.homepage.1' => [[], ['_controller' => 'Company\\Controller\\App\\HomepageController::index', '_locale' => 1, '_canonical_route' => 'app.homepage'], [], [['text', '/']], [['text', 'staging.app.domain.com']], [], []],
    'app.homepage.2' => [[], ['_controller' => 'Company\\Controller\\App\\HomepageController::index', '_locale' => 2, '_canonical_route' => 'app.homepage'], [], [['text', '/']], [['text', 'tests.app.domain.local']], [], []],
    'app.homepage.3' => [[], ['_controller' => 'Company\\Controller\\App\\HomepageController::index', '_locale' => 3, '_canonical_route' => 'app.homepage'], [], [['text', '/']], [['text', 'app.localhost']], [], []],

I think the canonical route is exactly what Symfony expects (it will fill the locale argument based on the HTTP host).

PS: I'm sure I had submitted this a couple weeks ago but can't find it... sorry if repost

@LauLaman
Copy link

LauLaman commented Aug 3, 2023

Same for multi locale routes:

#[Route([
    'nl' => '/boodschappen',
    'en' => '/groceries',
], name: 'dashboard')]
class DashboardController

Trying to use in a twig template

Screenshot 2023-08-03 at 21 26 35

instead of an indexed number we'll have an actual key

@BenoitDuffez
Copy link
Contributor Author

How can we help this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants