Pierre
Pierre

Reputation: 772

Symfony annotation routing order

I'm currently stuck with routing in my Symfony4 (4.3) project. My problem is pretty simple, I want to use route annotations in my controllers but I want to defined the order of these one.

For example, if I have two controllers with following routing :

class BarController extends AbstractController
{
    /**
     * @Route("/test/{data}", name="app_bar")
     */
    public function index($data)
    {
        // ...
        return $this->render('index.html.twig', [
            'data' => $data,
        ]);
    }
}

and

class FooController extends AbstractController
{
    /**
     * @Route("/test/my_value", name="app_foo")
     */
    public function index()
    {
        // ...
        return $this->render('index.html.twig', [
            'data' => 'my_value',
        ]);
    }
}

In config/routes/annotations.yaml I define my route like this

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

Then if I call /test/my_value I would like to be redirected to FooController since his index action define @Route("/test/my_value", name="app_foo") but like routes is loaded alphabetically the index action from BarController with app_bar route is called first.

So I have tried to defined following routing :

app_foo_controller:
    resource: ../../src/Controller/FooController.php
    type: annotation
app_controllers:
    resource: ../../src/Controller/
    type: annotation

But this didn't work, BarController and his app_bar route still called before app_foo route from FooController.

Also, I don't understand the purpose of config/routes/annotations.yaml vs config/routes.yaml since both could contains any type of routes... I miss something ?

Upvotes: 1

Views: 702

Answers (1)

Pierre
Pierre

Reputation: 772

Nevermind I found the solution. I just miss the fact that I override my specifically app_foo_controller routing when I define app_controllers the solution is to defined each controllers like that :

app_controllers:
    resource: ../../src/Controller/
    type: annotation
app_foo_controller:
    resource: ../../src/Controller/FooController.php
    type: annotation
app_bar_controller:
    resource: ../../src/Controller/BarController.php
    type: annotation

Upvotes: 1

Related Questions