1

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 ?

Pierre
  • 776
  • 6
  • 27
  • I would personally try and avoid a url structure where the same path sections are served by different controllers. If you do it that way around you can define the order w/o yaml files by just ordering the methods accordingly – Bananaapple Jun 05 '19 at 08:16
  • In my case I can't use the same controller because I have too many methods relies to specific case (`/add/{generic_case}` and `/add/specific1`, `/add/specific2`, ... `/add/specific1000` so I prefer put the specific actions in specific controller) , but yes indeed it's more simple to order actions in the same controller. – Pierre Jun 05 '19 at 09:04

1 Answers1

1

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
Pierre
  • 776
  • 6
  • 27
  • another option would be to add condition on `BarController` route so it does not get triggered, and `config/routes/annotations.yaml` is nothing more than sugar for keeping the structure more clear, it's not for functional point of view, you can name it anything you want – Ludo Jun 05 '19 at 08:43