How to simulate a REPL for Symfony applications
23 July 2025 (Updated 23 July 2025)
You can simulate a REPL like Laravel’s php artisan
or Rails’ rails console
by creating a controller only for debugging purposes.
For example, create a src/Controller/Debug/DebugController.php
file:
<?php
declare(strict_types=1);
namespace App\Controller\Debug;
use App\Entity\Company;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class DebugController extends AbstractController
{
public function __construct(
private EntityManagerInterface $entityManager,
) {
}
// http://localhost:8080/debug
#[Route('/debug', condition: '"%kernel.environment%" === "dev"')]
public function debug(): Response
{
$company = $this->entityManager->getRepository(Company::class)->find(665);
dd($company);
}
}
Don’t track your debug controller in Git by adding it to the .git/info/exclude
file (see this post for more info).
Open the .git/info/exclude
file:
vim .git/info/exclude
Add your debug controller’s file pattern:
/src/Controller/Debug
Add a browser bookmark or set up a system-wide hotkey to quickly launch your debug route (http://localhost:8080/debug
in the example above).
Tagged:
Symfony recipes