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
The package auto-registers and works immediately in APP_DEBUG=true environments.
First Use Case:
<!-- /resources/views/layouts/app.blade.php -->
These wrap the rendered output, showing the Blade file path responsible.Config: Publish the config (optional) for customization:
php artisan vendor:publish --tag="blade-comments-config"
Key settings:
enable: Toggles comments (defaults to APP_DEBUG).excludes.includes: Skip specific @include directives (e.g., ['partials.sidebar']).excludes.sections: Skip @yield sections (e.g., ['meta']).Middleware: The package adds AddRequestComments middleware automatically. Verify it’s registered in app/Http/Kernel.php under the web middleware group.
Debugging Blade Output:
@component('user-card')) are auto-detected and commented.Excluding Noise:
'excludes' => [
'includes' => ['styles.theme', 'scripts.footer'],
],
@yield directives in meta tags:
'excludes' => [
'sections' => ['meta', 'title'],
],
Custom Comments:
BladeCommenter:
namespace App\BladeComments;
use Spatie\BladeComments\Commenters\BladeCommenters\BladeCommenter;
class CustomDirectiveCommenter implements BladeCommenter {
public function pattern(): string {
return '/@custom\(.*?\)\s*/';
}
public function replacement(): string {
return '<!-- @custom directive -->$0<!-- /@custom directive -->';
}
}
Register in config/blade-comments.php:
'blade_commenters' => [
// ... default commenters
App\BladeComments\CustomDirectiveCommenter::class,
],
Request Metadata:
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()} -->";
}
}
Register in config:
'request_commenters' => [
// ... default commenters
App\BladeComments\UserAgentCommenter::class,
],
@component('alert')).APP_DEBUG=false or mocking the middleware.Performance:
APP_DEBUG=true. Ensure it’s disabled in production ('enable' => false).Edge Cases:
@include_if may not work as expected on Windows. Use @include explicitly if issues arise.@include($dynamicPath)). Exclude these paths in config.Middleware Order:
AddRequestComments runs after ShareErrorsFromSession and before VerifyCsrfToken in app/Http/Kernel.php to avoid CSRF token issues.Missing Comments:
'enable' => true and APP_DEBUG=true.php artisan view:clear
Livewire Issues:
>=2.0.3). Older versions may need manual component class resolution.Custom Patterns:
BladeCommenter, test patterns with preg_match to avoid infinite loops or missed matches.$0 to preserve matched content (e.g., replacement(): string { return '<!-- $0 -->'; }).Precompiler:
BladeCommentsPrecompiler to customize how comments are injected (e.g., add timestamps or user context).Event Hooks:
blade.comments.added events to log or modify comments dynamically:
use Spatie\BladeComments\Events\CommentsAdded;
CommentsAdded::listen(function (CommentsAdded $event) {
// Log or transform comments
});
Conditional Comments:
RequestCommenter to add environment-specific comments (e.g., show staging warnings):
public function comment(Request $request, Response $response): ?string {
return app()->environment('staging')
? '<!-- STAGING ENVIRONMENT -->'
: null;
}
How can I help you explore Laravel packages today?