php – Dynamic change of requirements for route. Symfony 3.3

Question:

Is it possible to dynamically change the requirements parameter for @Route ?

What I mean:

  1. The database contains a list of services that has its own url (service1, service2, etc.)
  2. There is a template that will be used in this case.

I have a similar Action:

/**
 * @Route(
 *     "/{service_url}/",
 *     requirements = {
 *         "service_url": "service1|service2|service3|service4"
 *     }
 * )
 */
public function blablaAction($service_url)
{
    ...code...
}

Can I change this parameter dynamically?

Those. added a new service to the database, get the url of the new service and add it to the requirements parameter. You can, of course, add a new service "by hand" each time, but this will be quite inconvenient, because. I want such an action to take place in the future without the participation of a programmer.

Answer:

Naturally this is possible. Or rather, I think this is naturally possible, but I did not do it myself. Let's go in order.

One of the answers referred to Symfony CMF and their dynamic router. If you open this documentation page, you will see what they write there in general about what I will write to you about below. Here is what they write there https://symfony.com/doc/3.3/routing/routing_from_database.html It's not magic, it's just a custom loader.

This is generally not a problem and never has been. Now I will explain everything to you.

See. Symfony is an extremely flexible system. At its core, Symfony is a collection of independent components. You need to take a closer look at the Routing component.

If you carefully read the documentation, you will notice that, for example, you can create your own "custom" route loader (I will call them in Russian that way). And now, realizing that you can create your own loader, you understand that you can load routes from anywhere and you can have them of any configuration. Be it a file, be it a database, be it something that came from outer space, and naturally your routes can respond to the state of your system, even the temperature of the environment.

You also need to understand that annotations are one of the descriptions of routes that are loaded by the route loader… right! which loads the routes described in the annotations.

Let's get down to business now. You need to make routes that depend on models. There is nothing easier! Open the documentation here . Watch how a custom Loader is created, create a service with the routing.loader tag and add it to the description of your routes in (routing.yml). Everything! You can add anything to this service and describe the routes as you like. Left, right, top and bottom! Don't forget to just remove the existing annotations in the controller and manage your routes in your own Loader

We have a dry balance.

  1. Create your own loader according to the documentation . (In its constructor you can add an EntityManager)
  2. Describe routes depending on your wishes (taking data from the same EntityManager)
  3. Add it to routing.yml
  4. Rejoice.

Once again the link to the documentation, it lives here https://symfony.com/doc/3.3/routing/custom_route_loader.html

p.s. Last. according to documentation

The routes defined using custom route loaders will be automatically cached by the framework. So whenever you change something in the loader class itself, don't forget to clear the cache.

This shouldn't confuse you. Naturally, all the logic inside the service remains dynamic, but the cache will need to be cleared after changes (as I understand it, in dev mode too)

Scroll to Top