-2

What I want to achieve is to get the controllers and routes as annotations from an Api folder created ad-hoc but it seems to not work correctly on symfony 5.1

Here is my config

services:
   App\Controller\:
      resource: '../../src/Controller'
      tags: ['controller.service_arguments']

   App\Api\:
      resource: '../../src/Api'
      tags: [ 'controller.service_arguments' ]

Api controller and route which is not matched

namespace App\Api;

/**
 * @Route("/api", name="api")
 */
 class ApiController extends AbstractController
 {

   /**
   * @Route("{id}", methods="HEAD", options={"title":"Api Root"})
   */
   public function head()
   {
      // TODO: Implement head() method.
   }

   /**
    * @Route("{id}", methods="GET", options={"title":"Api Root"})
    */
    public function index()
    {
       // NEVER CALLED
       return "hello";
    }
}

The route is not triggered and i get and exception. I'm missing something ?

Claudio Ferraro
  • 4,551
  • 6
  • 43
  • 78
  • What you are missing is that the services.yaml stuff is only for locating and tagging controllers. Nothing to do with routes. You don't need them at all in fact since you are extending from AbstractController. Take a peak at config/routes/annotations.yaml And use 'bin/console debug:router' to verify your routes are defined. – Cerad Nov 16 '20 at 14:18
  • Why I dont need them at all ? Can you explain ? It means that I can specify anything as an action ? – Claudio Ferraro Nov 17 '20 at 08:29
  • Anyway I think that my example was clear enough and I already got an answer. I pasted the answer below. – Claudio Ferraro Nov 17 '20 at 13:02
  • Any class that extends from AbstractController is automatically tagged as a controller and it's methods are treated as actions. It's also possible to make controllers that do not extend AbstractController. Those classes need to be tagged. – Cerad Nov 17 '20 at 13:03
  • Thank you. I think I'm doing something wrong then because even though I extend AbstractController I don't get the routes – Claudio Ferraro Nov 17 '20 at 13:05
  • Your solution below is correct. It's why I suggested looking at annotations.yaml. You have a conceptual problem in that you are assuming that routes are automatically extracted from all controllers. They are not. Two different processes. The controller.service_arguments tag is what allows Symfony to inject dependencies into controller actions. Nothing to do with routing. Don't worry if you don't fully understand the difference yet. You are doing things correctly and eventually it will sink in. – Cerad Nov 17 '20 at 13:13

1 Answers1

-1

Guys thank you for your help, anyway I think since I was using annotations i was missing this in the annotations.yaml file:

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

api_controllers:
    resource: ../../src/Api/
    type: annotation

api_controllers can be anything you prefer but should be of type: annotation

Claudio Ferraro
  • 4,551
  • 6
  • 43
  • 78