Display Configured Routes using Console Command in Symfony 7

Display Configured Routes using Console Command in Symfony 7

Symfony framework provides various built-in commands for debugging and development purpose. This tutorial shows how to use console command to display configured routes in Symfony 7 application.

Let's say we have the following controller that has one method which defines a route for the /test URL:

src/Controller/TestController.php

<?php

namespace App\Controller;

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

class TestController
{
    #[Route('/test', name: 'test_index')]
    public function index(): Response
    {
        return new Response();
    }
}

To get all configured routes in Symfony application, we can use debug:router command. Routes are displayed in the same order in which evaluated by Symfony.

php bin/console debug:router

Output example:

-------------- -------- -------- ------ -----------  
 Name           Method   Scheme   Host   Path        
-------------- -------- -------- ------ -----------
 test_index     ANY      ANY      ANY    /test
-------------- -------- -------- ------ -----------

The --show-controllers option can be used to add additional column to the output to display associated controllers with routes.

php bin/console debug:router --show-controllers

Output example:

-------------- -------- -------- ------ ----------- ----------------------------------------  
 Name           Method   Scheme   Host   Path        Controller                               
-------------- -------- -------- ------ ----------- ----------------------------------------
 test_index     ANY      ANY      ANY    /test       App\Controller\TestController::index()
-------------- -------- -------- ------ ----------- ----------------------------------------

To get details about an individual route, we can provide route name as argument.

php bin/console debug:router test_index

Output example:

+--------------+---------------------------------------------------------+ 
| Property     | Value                                                   | 
+--------------+---------------------------------------------------------+
| Route Name   | test_index                                              |
| Path         | /test                                                   |
| Path Regex   | {^/test$}sDu                                            |
| Host         | ANY                                                     |
| Host Regex   |                                                         |
| Scheme       | ANY                                                     |
| Method       | ANY                                                     |
| Requirements | NO CUSTOM                                               |
| Class        | Symfony\Component\Routing\Route                         |
| Defaults     | _controller: App\Controller\TestController::index()     |
| Options      | compiler_class: Symfony\Component\Routing\RouteCompiler |
|              | utf8: true                                              |
+--------------+---------------------------------------------------------+

Leave a Comment

Cancel reply

Your email address will not be published.