skip to Main Content

Ihave a symfony app that ignore the roytes i can declare.
For exemple, i have this lines of code in routes.yaml :

app_titi
    path: /titi
    methods: ['GET']
    defaults:
        _controller: 'AppControllerTitiController::index'

I do a cache:clear and when i try to watch the result in my browser, no route found.

The controller exists and have the right name.

My context is Symfony 6.2, PHP 8.1, runing in Docker containers.

I tryed to create a new controller, i declared it in routes.yaml, same results.

I tryed to create a controller but this time using annotations, same results.

When i ask the command router:debug, symfony console returns an empty results.

Thanks for helping!

2

Answers


  1. Chosen as BEST ANSWER

    @ThomasL

    Hi, here is my routes.yaml :

    controllers:
        resource:
            path: ../src/Controller/
            namespace: AppController
        type: attribute
    
    app_titi:
        path: /titi
        methods: ['GET']
        defaults:
            _controller: 'AppControllerTitiController::index'
    

    Others routes are now declared in attributes.

    This is my Titi controller :

    <?php
    
    namespace AppController;
    
    use SymfonyBundleFrameworkBundleControllerAbstractController;
    use SymfonyComponentHttpFoundationResponse;
    use SymfonyComponentRoutingAnnotationRoute;
    
    class TitiController extends AbstractController
    {
        #[Route('/titi', name: 'app_titi')]
        public function index(): Response
        {
            return $this->render('titi/index.html.twig', [
                'controller_name' => 'TitiController',
            ]);
        }
    }
    
    

  2. For a better answer please provide your controller path and code example.

    In Symfony 6.2, by default, you do not have to change routes.yaml since all route declared in controller are auto detected if you keep this inside routes.yaml.
    Here is my routes.yaml for example :

    controllers:
        resource: ../src/Controller/
        type: annotation
    
    kernel:
        resource: ../src/Kernel.php
        type: annotation
    

    Then inside the controller

    <?php
    
    namespace AppController;
    
    
    use SymfonyBundleFrameworkBundleControllerAbstractController;
    use SymfonyComponentRoutingAnnotationRoute;
    
    class HomeController extends AbstractController
    {
    
        // This not annotation but php attribute, i advise you to use it instead
        #[Route('/', name: "home")]
        public function home(): Response
        {
            return $this->render('website/index.html.twig');
        }
    }
    

    Read this to understand why symfony now use php attribute :

    https://symfony.com/blog/new-in-symfony-5-2-php-8-attributes

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search