Tib
Tib

Reputation: 2631

Best way to change routes loading order

I have routes in my controller loaded in /config/routes/annotations.yaml

controllers:
     resource: ../../src/Controller/
     type: annotation

I have routes in /config/routes.yaml

about:
    path:         /about
    controller:   Symfony\Bundle\FrameworkBundle\Controller\TemplateController::templateAction
    defaults:
        template: front/about.html.twig
        ...

Dynamic routes in "controllers" are overriding my "static" routes.

What is the best way to load "static" routes before those in controllers.

I made it work by commenting the content in /config/routes/annotations.yaml and pasting it in the end of /config/routes.yaml but I don't feel it's the best way to do it...

Upvotes: 1

Views: 1685

Answers (3)

Mondane
Mondane

Reputation: 498

You can avoid changing the kernel logic by putting the controller with annotations after the static route in routes.xml:

browserconfig:
    path: /browserconfig.xml
    controller: Symfony\Bundle\FrameworkBundle\Controller\TemplateController::templateAction
    defaults:
        template: browserconfig.xml.twig

app_document:
    resource: App\Controller\DocumentController
    type: annotation

Upvotes: 0

Tib
Tib

Reputation: 2631

It didn't want to change my url nor using the alphabetical trick as suggested in the comments.

I fixed it by changing the order of imported routes in the kernel.

Instead of:

protected function configureRoutes(RouteCollectionBuilder $routes)
    {
        $confDir = $this->getProjectDir().'/config';

        $routes->import($confDir.'/{routes}/*'.self::CONFIG_EXTS, '/', 'glob');
        $routes->import($confDir.'/{routes}/'.$this->environment.'/**/*'.self::CONFIG_EXTS, '/', 'glob');
        $routes->import($confDir.'/{routes}'.self::CONFIG_EXTS, '/', 'glob');
    }

I

protected function configureRoutes(RouteCollectionBuilder $routes)
        {
            $confDir = $this->getProjectDir().'/config';
            # routes loaded in routes.yaml
            $routes->import($confDir.'/{routes}'.self::CONFIG_EXTS, '/', 'glob');
            # routes loaded in routes/annotations.yaml
            $routes->import($confDir.'/{routes}/*'.self::CONFIG_EXTS, '/', 'glob');
            $routes->import($confDir.'/{routes}/'.$this->environment.'/**/*'.self::CONFIG_EXTS, '/', 'glob');
        }

Upvotes: 1

Padam87
Padam87

Reputation: 1029

Technically this might not be enough though. Any route loaded after this will be ignored, and if you are using annotations, you have to put this action in the last action of the last controller sorted alphabetically.

Configure this route in yml, and put it at the end of routes.yml.

This route will be the last to execute (performance hit), and it will catch all requests, so make sure you throw 404-s properly.

(Am I right in assuming that the client wants to be able to configure routes completely? eg CMS pages? Had that situation a couple of times)

Upvotes: 1

Related Questions