skip to Main Content

I recently got a new laptop, and after I installed PHP 8.2.8 and Symfony, I thought I’d be able to start a new Symfony 5.4 webapp project but whenever I start the local server it doesn’t find the routes in my Controller.

I used the command php bin/console make:controller which has done the trick previously, but now doesn’t seem to get it to work.

I’ve tried looking for answers online but there’s no one who has had the same problem, as far as I know. I get the No route found for "GET http://127.0.0.1:8000/index" error while the following code is in my IndexController:

<?php

namespace AppController;

use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentRoutingAnnotationRoute;

class IndexController extends AbstractController
{
    #[Route('/index', name: 'app_index')]
    public function index(): Response
    {
        return $this->render('index/index.html.twig', [
            'controller_name' => 'IndexController',
        ]);
    }
}

Just the template, as you can see.

-------------------------- -------- -------- ------ -----------------------------------
  Name                       Method   Scheme   Host   Path
 -------------------------- -------- -------- ------ -----------------------------------
  _preview_error             ANY      ANY      ANY    /_error/{code}.{_format}
  _wdt                       ANY      ANY      ANY    /_wdt/{token}
  _profiler_home             ANY      ANY      ANY    /_profiler/
  _profiler_search           ANY      ANY      ANY    /_profiler/search
  _profiler_search_bar       ANY      ANY      ANY    /_profiler/search_bar
  _profiler_phpinfo          ANY      ANY      ANY    /_profiler/phpinfo
  _profiler_search_results   ANY      ANY      ANY    /_profiler/{token}/search/results
  _profiler_open_file        ANY      ANY      ANY    /_profiler/open
  _profiler                  ANY      ANY      ANY    /_profiler/{token}
  _profiler_router           ANY      ANY      ANY    /_profiler/{token}/router
  _profiler_exception        ANY      ANY      ANY    /_profiler/{token}/exception
  _profiler_exception_css    ANY      ANY      ANY    /_profiler/{token}/exception.css
 -------------------------- -------- -------- ------ -----------------------------------

These are the routes.

I feel like something might’ve gone wrong during the installation of Symfony, but I have no clue what’s gone wrong.

2

Answers


  1. You should set up router to parse attributes on files in the Controller directory. Paste this code to your config/routes/attributes.yaml:

    controllers:
        resource:
            path: ../../src/Controller/
            namespace: AppController
        type: attribute
    
    Login or Signup to reply.
  2. Make sure to execute bin/console cache:clear after every configuration change

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