trrrrrrm
trrrrrrm

Reputation: 11802

Symfony2: Routing priority

is there anyway to prioritize routes in Symfony2?

i'm using annotation it looks like this

Controllers

//TestController.php

/**
 * @Route("/test")
 */
class TestController extends Controller
{
    /**
     * @Route("/a", name="test_a")
     */
   .....

//DummyController.php
/**
 * @Route("/")
 */
class DummyController extends Controller
{
    /**
      * @Route("/{varA}/{varB}", name="dummy_one")
      */
   .....

Config

//routing.yml

acme_bundle:
    resource: "@Acme/Controller"
    type:     annotation

Goal

URL      , Actual              , Goal
/test/a  , DummyController     , TestController  //Wrong
/test/b  , DummyController     , DummyController //Good

How can i force TestController to be tested first ?

Thanks

Upvotes: 4

Views: 8567

Answers (4)

COil
COil

Reputation: 7596

As of Symfony 5.1, a priority can be assigned to each route:

#[Route(path: '/', name: 'home')]
public function home(): Response
{
    return $this->render('home.html.twig');
}

#[Route(path: '/about', name: 'about', priority: 10)]
public function about(): Response
{
    return $this->render('about.html.twig');
}

In this case (it's just a dummy example), the about route with a priority of 10 will be evaluated before the home route, which has the default priority of 0.

Check out the feature introduction on the Symfony blog.

Upvotes: 0

Siegfried DuDragon
Siegfried DuDragon

Reputation: 61

You don't need to reorder everything. Just overwrite the rules for DummyController at the desired ranking. You can do this by creating a route to the controller at the end of your routing.yml. So in your routing.yml as the last lines you will add:

app_dummy:
    ressource: "@YourBundle/Controller/DummyController.php
    type:     annotation

Upvotes: 0

trrrrrrm
trrrrrrm

Reputation: 11802

So Symfony will consume the controllers in an alphabetical order and will add the routes one by one.

there is no way up to add priority at the moment without using another bundle for that currently version 2.5

https://github.com/symfony-cmf/Routing is a great bundle if you are looking for advanced routing.

Upvotes: 6

Derick F
Derick F

Reputation: 2769

from your example i can assume that your dummy and test controller are in the same bundle, if this is the case then you just lists the controllers in that bundle individually in your routing.yml. the order you list them is the order they will be checked.

acme_test:
    resource: "@Acme/Controller/TestController.php"
    type:     annotation

acme_dummy:
    resource: "@Acme/Controller/DummyController.php"
    type:     annotation

if they are in different bundles, just list the bundle with the test controller first.

see the symfony routing doc for details. http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html#activation

Upvotes: 9

Related Questions