Find Route That Matches Given URL using Console Command in Symfony 7

Find Route That Matches Given URL using Console Command in Symfony 7

Symfony framework has many built-in commands which can be used to debug various application parts. This tutorial explains how to use console command to find the route that matches a given URL in Symfony 7 application.

We have a controller that has one method which defines a route for the /test URL. Route matches HTTP GET method.

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', methods: ['GET'])] public function index(): Response { return new Response(); } }

The router:match command can be used to find which route matches a given URL. This command accepts URL as argument.

php bin/console router:match /test

If a route was found, then the command prints the route name and detailed information about the route.

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

The --method, --scheme and --host options can be used to find the route that matches a given URL by HTTP method (e.g. POST, GET), scheme (e.g. HTTP or HTTPS) and host.

php bin/console router:match /test --method=get --scheme=http --host=localhost

Leave a Comment

Cancel reply

Your email address will not be published.