Search code examples
phpsymfonypsr-4symfony6

Cannot use a Symfony Controller placed on an alternative folder


I'm trying to place a controller on src/Web/Infraestructure instead of src/Controllers

<?php

namespace App\Web\Infraestructure;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class FooController
{
    #[Route('/foo', name: 'foo_route')]
    public function number(): Response
    {
        return new Response(
            '<html><body>Hello From Foo!!!</body></html>'
        );
    }
}

on composer.json I have the defaulf config

 "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\\Tests\\": "tests/"
        }
    },

But I get this error when the controller is placed in src/Web/Infraestructure

No route found for "GET http://127.0.0.1:8000/foo"

Solution

  • In casse you are trying to create a route inside another folder than src/Controller. You have to modify the attributes of the route's configuration. Have a look at this link

    If you are not sure about if the route exists, look over the router to search the /foo route with the following command:

    php bin/console debug:router