php - Symfony wildcard host route with no named parameter


I want to host 2 different domains inside a single Symfony 4 application, so I'm using@Route'shost parameter to declare the domain each page belongs to:

 * @Route("/foo", name="foo", host="")

This works fine; however, for my local dev server, I will typically use a domain instead. So the route needs to match multiple extensions. I tried using a named placeholder for this purpose:

 * @Route("/foo", name="foo", host="example.{ext}")

This works fine for routing, but not for URL generation. For example, if a Twig template attempts to use{{ path('foo') }}, I now get the following error:

Some mandatory parameters are missing ("ext") to generate a URL for route "foo".

Is there a way to add a wildcard for the host, while still allowing route generation without passing a parameter?

I know the question sounds odd, as routing must be bi-directional, but how is this typically handled when one needs to have a dev environment with different domains?

Is there maybe a way to provide a global, default value for theext parameter?




Found it:

 * @Route("/foo", name="foo", host="example.{ext}", defaults={"ext": "%ext%"})

And configure the default value inconfig/services.yaml:

    ext: 'com'

Or better yet, use a constant:

    ext: '%env(DOMAIN_EXT)%'

And define it in your.env and/or.env.local as needed:



It may be even better to allow the whole host to be configurable. In this case, it looks like:

 * @Route("/foo", name="foo", host="")

And as above, configure the domain name inconfig/services.yaml, with or without a constant:

parameters: '%env(DOMAIN_NAME)%'

People are also looking for solutions to the problem: php - Laravel 6 | Configurable Jobs, attaching a model to a job


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.