raulfraile/ladybug-bundle
LadybugBundle adds an easy, extensible var_dump/print_r replacement for Symfony2. Use ladybug_dump()/ld() in controllers or the ladybug_dump Twig filter to inspect strings, arrays, objects, and resources with clean, readable output.
composer require raulfraile/ladybug-bundle
config/bundles.php:
return [
// ...
Raulfraile\LadybugBundle\RaulfraileLadybugBundle::class => ['all' => true],
];
use Raulfraile\LadybugBundle\Ladybug\Ladybug;
public function index()
{
$data = ['name' => 'John', 'active' => true];
Ladybug::dump($data); // or use the helper: `ld($data)`
}
|ladybug_dump filter:
{{ dumpable_variable|ladybug_dump }}
Debugging Controllers/Commands:
Replace var_dump() or dd() with Ladybug::dump() for structured output.
public function show(User $user)
{
Ladybug::dump($user->toArray()); // Pretty-printed array
}
Twig Debugging: Use the filter in templates to inspect variables:
{% for item in items %}
{{ item|ladybug_dump }}
{% endfor %}
Service/Repository Debugging:
Inject Ladybug service and dump complex objects:
public function __construct(private Ladybug $ladybug) {}
public function fetchData()
{
$result = $this->repository->findAll();
$this->ladybug->dump($result);
return $result;
}
CLI Debugging: Use in console commands for real-time inspection:
use Symfony\Component\Console\Command\Command;
use Raulfraile\LadybugBundle\Ladybug\Ladybug;
class DebugCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output)
{
$data = $this->getData();
Ladybug::dump($data, $output); // Stream to CLI
}
}
Ladybug\Handler\HandlerInterface to handle specific types (e.g., Doctrine entities).config/packages/raulfraile_ladybug.yaml:
raulfraile_ladybug:
handlers:
- { service: 'app.custom_handler' }
max_depth: 5
app['kernel']->getEnvironment():
if ($this->getParameter('kernel.environment') === 'prod') {
return;
}
Ladybug::dump($data);
Performance Overhead:
if ($debug) guards.if ($this->getParameter('kernel.debug')) {
Ladybug::dump($expensiveQueryResult);
}
Circular References:
raulfraile_ladybug:
max_depth: 3
Twig Filter Scope:
|ladybug_dump filter only works in Twig if the bundle is enabled and the LadybugTwigExtension is registered (it is by default).Output Buffering:
Ladybug::dump() (e.g., avoid echo or response->send() beforehand).Deprecated Methods:
Ladybug::dump() over ld() helper in new code (though both work).$handlers = $this->container->get('raulfraile_ladybug.handler.registry')->getHandlers();
@RaulfraileLadybug/debug.html.twig).Ladybug::dump($data, new \Symfony\Component\Console\Output\BufferedOutput());
file_put_contents('debug.log', $output->fetch());
Add Custom Handlers:
Create a service tagged as raulfraile_ladybug.handler:
services:
app.custom_handler:
class: App\Handler\CustomHandler
tags:
- { name: 'raulfraile_ladybug.handler', priority: 100 }
Implement Raulfraile\LadybugBundle\Ladybug\Handler\HandlerInterface.
Override Templates:
Copy vendor/raulfraile/ladybug-bundle/Resources/views/ to templates/bundles/raulfraileladybug/ and modify.
Hook into Dump Events:
Listen for ladybug.dump events to modify output dynamically:
$dispatcher->addListener('ladybug.dump', function ($event) {
$event->setData(['custom_key' => 'value']);
});
How can I help you explore Laravel packages today?