spatie/laravel-blade-comments
Adds HTML debug comments around every rendered Blade view/component so you can see exactly which template produced each piece of output in browser dev tools. Also includes top-level request and view info at the top of the document.
Installation:
composer require spatie/laravel-blade-comments --dev
APP_DEBUG mode (configurable via blade-comments.enable).First Use Case:
<!-- BEGIN: resources/views/layouts/app.blade.php -->
<!-- END: resources/views/layouts/app.blade.php -->
Where to Look First:
config/blade-comments.php to adjust exclusions or middleware.Spatie\BladeComments\Http\Middleware\AddRequestComments is registered in app/Http/Kernel.php (it is by default).excludes.includes and excludes.sections to avoid cluttering partials or meta tags.Debugging Blade Renders:
<!-- BEGIN: resources/views/home.blade.php -->) to identify the root view.@component('widget.card')) are auto-detected and commented.@yield('section-name') directives are wrapped with comments, even in nested layouts.Integration with Existing Tools:
APP_DEBUG=false or overriding blade-comments.enable=false in .env.Custom Commenters:
@myDirective):
namespace App\BladeComments;
use Spatie\BladeComments\Commenters\BladeCommenters\BladeCommenter;
class MyDirectiveCommenter implements BladeCommenter {
public function pattern(): string { return '/@myDirective\s*(.*?)\s*/'; }
public function replacement(): string { return '<!-- BEGIN: @myDirective $1 -->$0<!-- END: @myDirective -->'; }
}
Register in config/blade-comments.php:
'blade_commenters' => [
// ... default commenters
App\BladeComments\MyDirectiveCommenter::class,
],
Request Metadata:
RequestCommenter to inject custom data (e.g., user agent, request duration):
namespace App\BladeComments;
use Spatie\BladeComments\Commenters\RequestCommenters\RequestCommenter;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class UserAgentCommenter implements RequestCommenter {
public function comment(Request $request, Response $response): ?string {
return "<!-- User Agent: {$request->userAgent()} -->";
}
}
Add to request_commenters in config.APP_DEBUG=true). Zero overhead in production.BladeComponentCommenter (no manual setup needed).excludes.includes for CSS/JS partials (e.g., 'styles.main') to avoid noise.False Positives in Excludes:
excludes.includes (e.g., 'header' might exclude 'header.partial').'partials.header') or regex patterns in the config:
'excludes' => [
'includes' => [
'/^styles\./', // Exclude all files starting with "styles."
],
],
Conditional Includes:
@include('partial', ['key' => $value]) may fail if $value is undefined.try-catch in custom commenters.Middleware Order:
AddRequestComments runs after the response is sent.StartSession and ShareErrorsFromSession in Kernel.php:
protected $middleware = [
// ...
\Spatie\BladeComments\Http\Middleware\AddRequestComments::class,
// ...
];
Livewire Component Namespaces:
App\Livewire\Admin\Dashboard) may not resolve correctly.LivewireComponentCommenter to handle namespaced classes:
class CustomLivewireCommenter extends \Spatie\BladeComments\Commenters\BladeCommenters\LivewireComponentCommenter {
protected function resolveClass(string $class): ?string {
return class_exists($class) ? $class : null;
}
}
Missing Comments:
APP_DEBUG=true in .env.Kernel.php.php artisan config:clear if config changes aren’t reflected.Malformed Comments:
php artisan view:clear to regenerate cached views.Dynamic Exclusions:
excludes via a service provider:
public function boot() {
config(['blade-comments.excludes.includes' => array_merge(
config('blade-comments.excludes.includes'),
['dynamic.partial']
)]);
}
Conditional Commenting:
Route::middleware(['web', 'blade-comments:disable'])->group(function () {
// Comments will be skipped here
});
Add this middleware to Kernel.php:
protected $routeMiddleware = [
'blade-comments.disable' => \Spatie\BladeComments\Http\Middleware\DisableBladeComments::class,
];
Custom Comment Formatting:
BladeCommentsPrecompiler:
class CustomPrecompiler extends \Spatie\BladeComments\BladeCommentsPrecompiler {
protected function wrapWithComments(string $content, string $path): string {
return "<!-- CUSTOM: $path -->$content<!-- /CUSTOM -->";
}
}
Update the config:
'precompiler' => App\BladeComments\CustomPrecompiler::class,
php artisan debugbar:enable
APP_DEBUG). No impact on view:cache.How can I help you explore Laravel packages today?