spatie/laravel-blade-comments
Adds HTML debug comments around every rendered Blade view/component so you can see exactly which template produced each part of the page in your browser dev tools. Also includes request and top-level view info at the top of the document.
Installation:
composer require spatie/laravel-blade-comments
Publish the config (optional):
php artisan vendor:publish --provider="Spatie\BladeComments\BladeCommentsServiceProvider"
First Use Case:
Enable the package in config/blade-comments.php:
'enabled' => env('BLADER_COMMENTS_ENABLED', false),
Set the environment variable:
BLADER_COMMENTS_ENABLED=true
Restart your dev server. Now inspect any rendered Blade view in your browser’s dev tools—you’ll see comments like:
<!-- Blade view: resources/views/layouts/app.blade.php -->
config/blade-comments.php (customize comment style, exclusions, or enabled routes).Spatie\BladeComments\BladeCommentsServiceProvider (registers middleware).Spatie\BladeComments\Middleware\AddBladeComments (core logic).Enable for Specific Routes:
Use middleware in routes/web.php:
Route::middleware(['web', 'blade.comments'])->group(function () {
// Routes where comments are enabled
});
Conditional Rendering:
Disable comments for specific views in config/blade-comments.php:
'exclude_views' => [
'layouts.app',
'emails.*',
],
Custom Comment Styling: Override the default comment template in the config:
'comment_template' => '<!-- [VIEW: {{view}}] [LINE: {{line}}] -->',
Integration with Debugging Tools:
Combine with laravel-debugbar or telescope for deeper insights:
// In Telescope config
'panels' => [
\Spatie\BladeComments\Telescope\BladeCommentsPanel::class,
],
Dynamic Enablement: Toggle comments via a route parameter:
Route::get('/debug', function () {
config(['blade-comments.enabled' => request('enable') === 'true']);
return view('home');
});
Component-Specific Comments:
Use @comment directive in Blade:
@comment('Custom section label')
<div>...</div>
CI/CD Exclusion:
Disable in production via .env:
BLADER_COMMENTS_ENABLED=${APP_ENV !== 'local'}
Performance Overhead:
'enabled' => app()->environment(['local', 'staging']),
False Positives in Exclusions:
exclude_views may unintentionally skip critical views. Test exclusions:
'exclude_views' => [
'auth.*', // Excludes all auth views
],
Caching Conflicts:
'cache_views' => false, // In config/app.php
Nested Component Comments:
@comment sparingly.Verify Middleware:
Check if the middleware is registered in App\Http\Kernel.php:
protected $middlewareGroups = [
'web' => [
\Spatie\BladeComments\Middleware\AddBladeComments::class,
// ...
],
];
Inspect Output:
Search for <!-- Blade view: in the page source to confirm comments are rendering.
Log Exclusions: Add debug logging for excluded views:
'log_excluded_views' => true, // In config
Custom Directives:
Extend the package by adding a directive (e.g., @debug):
// In a service provider
Blade::directive('debug', function ($expression) {
return "<?php echo '<!-- DEBUG: {$expression} -->'; ?>";
});
Event Listeners:
Listen for blade.comments.rendered events to post-process comments:
Event::listen('blade.comments.rendered', function ($view, $content) {
$content = str_replace('<!-- Blade view:', '<!-- CUSTOM: ', $content);
return $content;
});
Testing: Mock comments in PHPUnit:
$this->withoutMiddleware(\Spatie\BladeComments\Middleware\AddBladeComments::class);
How can I help you explore Laravel packages today?