Installation:
composer require cg/kint-bundle
Ensure Cg\KintBundle\CgKintBundle::class is registered in config/bundles.php under the dev environment.
Enable in Dev Environment:
# config/packages/dev/cg_kint.yaml
cg_kint:
enabled: true
First Use Case:
In any Twig template, replace {{ dump(var) }} with:
{{ kint(user) }}
Refresh the page to see a structured, collapsible dump of the user variable.
Variable Inspection:
Use {{ kint(variable) }} in templates to inspect objects, arrays, or complex data structures. Group related variables:
{{ kint(user, request, app.container.get('service')) }}
Conditional Debugging:
Wrap Kint in Twig if blocks for environment-specific debugging:
{% if app.environment == 'dev' %}
{{ kint(form.vars.data) }}
{% endif %}
Controller Integration: Pass data from controllers to templates for debugging:
// src/Controller/DebugController.php
public function showDebug(DebugInterface $debugService)
{
return $this->render('debug/index.html.twig', [
'debugData' => $debugService->getData(),
]);
}
Custom Twig Extensions: Extend Kint functionality by creating a custom Twig extension:
// src/Twig/AppExtension.php
public function getFunctions()
{
return [
new \Twig\TwigFunction('custom_kint', [$this, 'customKintDump']),
];
}
public function customKintDump($var, $label = null)
{
return \Kint::dump($var, $label);
}
Register the extension in services.yaml:
services:
App\Twig\AppExtension:
tags: ['twig.extension']
Performance Overhead:
{{ kint($allUsers) }} where $allUsers is a 10k-record collection).Twig Cache Conflicts:
php bin/console cache:clear
Recursive Data Limits:
cg_kint:
max_depth: 5 # Default is 3
Resource Handling:
fopen()) may display raw paths. Use kint() cautiously with untrusted data to avoid leaks.Filtering Output: Use Kint’s built-in filters for cleaner output:
{{ kint(user|slice(0, 5)) }} {# Show first 5 items of a collection #}
Grouping Variables: Group related dumps for clarity:
{{ kint([
'User Data': user,
'Request Data': request.attributes,
'Session Data': app.session.all()
]) }}
Command-Line Debugging:
Combine with Symfony’s debug:container or debug:router for deeper inspection:
php bin/console debug:container App\Service\DebugService
Extension Points:
Kint\Renderer\RendererInterface in services.yaml:
services:
App\Kint\CustomRenderer:
tags: ['kint.renderer']
Legacy Symfony Versions:
kint-twig or symfony/web-profiler-bundle as alternatives.Styling Conflicts:
/* assets/css/kint-override.css */
.kint-container { background: #f8f8f8; }
How can I help you explore Laravel packages today?