Make Route Available if Debug Mode is Enabled in Symfony 7

Make Route Available if Debug Mode is Enabled in Symfony 7

Symfony framework allows restricting routes through conditions. This tutorial explains how to make route available if debug mode is enabled in Symfony 7 application.

In .env file, we can specify the APP_ENV variable which defines the environment in which the application runs.

.env

APP_ENV=dev

Generally, in the dev and test environments, debug mode is enabled and in the prod environment is disabled.

The conditions routing option allows defining expression for route matching. We can use kernel.debug container parameter to define an expression to make route available only if debug mode is enabled.

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', condition: '%kernel.debug% === 1')]
    public function index(): Response
    {
        return new Response();
    }
}

config/routes.yaml

test:
    path: /test
    controller: App\Controller\TestController::index
    condition: "%kernel.debug% === 1"

Leave a Comment

Cancel reply

Your email address will not be published.