4

i'm running a symfony application on Homestead, but when adding a route by annotation for simple action it doesn't work.

namespace App\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class BlogController extends AbstractController
{
/**
 * @Route("/index",name="blog_index")
 */
public function index(){
    dd('Test KO');
  }
}

for routes debugger

vagrant@homestead:~/code/symfony61$ php bin/console debug:router
 -------------------------- -------- -------- ------ ----------------------------------- 
  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_xdebug           ANY      ANY      ANY    /_profiler/xdebug                  
  _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   
 -------------------------- -------- -------- ------ ----------------------------------- 
Badr MOUMOUD
  • 166
  • 2
  • 10

4 Answers4

1

Symfony 6 is not coming with preconfigured route annotation config. So you need to follow this easy steps:

1- create a attributes.yaml file under config/routes folder.

2- add following code

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

3- add your route annotation with new syntax like

#[Route('/inbound', name: 'inbound', methods: 'POST')]
public function inbound(Request $request): Response
{
    return new Response('OK', 200);
}

4- Debug your route if everything is ok

bin/console debug:router
Kaslico
  • 3,778
  • 3
  • 12
  • 10
0

There is usually a file: config/routes.yaml with the content:

controllers:
    resource: ../src/Controller/
    type: annotation
    # (the type: annotation option also applies to attributes...)

See: https://symfony.com/doc/current/routing.html#creating-routes-as-attributes-or-annotations

Alister Bulman
  • 34,482
  • 9
  • 71
  • 110
0

PHP8 + Symfony 6 use PHP annotations: #[Route('/', name: 'app_index')]

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 21 '22 at 10:05
0

In symfony 6 and PHP 8, You can use PHP attributes instead of symfony annotation. Use like this.

#[Route('/index', name: 'blog_index')]
public function blog_index(){
    //code..
  }
}