Installation Add the package via Composer:
composer require c4/frontend-bundle
Publish the default configuration (if needed):
php artisan vendor:publish --provider="C4\FrontendBundle\FrontendBundle" --tag="config"
Basic Configuration
Update config/frontend.php to define your frontend assets (CSS/JS) and their sources (e.g., local, CDN, or versioned files):
'assets' => [
'css' => [
'app' => [
'path' => 'assets/css/app.css',
'version' => '1.0.0',
],
],
'js' => [
'app' => [
'path' => 'assets/js/app.js',
'version' => '1.0.0',
],
],
],
First Use Case: Rendering Assets
Use the Frontend facade to generate HTML tags for assets:
use C4\FrontendBundle\Facades\Frontend;
// In a Blade template or controller
echo Frontend::css('app'); // <link href="/assets/css/app.css?v=1.0.0" rel="stylesheet">
echo Frontend::js('app'); // <script src="/assets/js/app.js?v=1.0.0"></script>
Blade Directives (Optional)
Add Blade directives for convenience (define in app/Providers/AppServiceProvider):
Blade::directive('css', function ($asset) {
return "<?php echo \\C4\\FrontendBundle\\Facades\\Frontend::css('$asset'); ?>";
});
Blade::directive('js', function ($asset) {
return "<?php echo \\C4\\FrontendBundle\\Facades\\Frontend::js('$asset'); ?>";
});
Now use in Blade:
@css('app')
@js('app')
admin, dashboard) in the config:
'assets' => [
'css' => [
'admin' => ['path' => 'admin/css/style.css'],
'dashboard' => ['path' => 'dashboard/css/main.css'],
],
],
Render them conditionally:
if (auth()->user()->isAdmin()) {
echo Frontend::css('admin');
}
'assets' => [
'js' => [
'vendor' => [
'path' => 'vendor.js',
'version' => env('APP_VERSION', '1.0.0'), // Dynamic versioning
],
],
],
'assets' => [
'js' => [
'analytics' => [
'path' => 'analytics.js',
'version' => false, // No versioning
],
],
],
url key to point to external sources:
'assets' => [
'css' => [
'bootstrap' => [
'url' => 'https://cdn.example.com/bootstrap.css',
'version' => '5.3.0',
],
],
],
echo Frontend::css('bootstrap', [
'cdn_fallback' => asset('vendor/bootstrap.css'),
]);
// In a middleware
public function handle($request, Closure $next) {
if ($request->routeIs('admin.*')) {
app('frontend')->addAsset('css', 'admin');
}
return $next($request);
}
'assets' => [
'css' => [
'app' => [
'path' => mix('css/app.css'), // Mix output
],
],
],
publicPath in webpack.mix.js matches the bundle’s expected path.'assets' => [
'css' => [
'theme' => [
'path' => config('app.theme') . '/style.css',
],
],
],
Missing Configuration
config/frontend.php.Frontend::css()/Frontend::js() calls.Versioning Conflicts
env('APP_VERSION')) or let the bundle auto-generate hashes:
'version' => 'auto', // Uses filemtime for versioning
Blade Directive Scope
AppServiceProvider is not registered.config/app.php under providers.Asset Path Resolution
publicPath is misconfigured.asset() helper or set the base path in the config:
'base_path' => public_path('assets'),
Overwriting Defaults
php artisan vendor:publish --tag="config" --force
Check Generated HTML
Use Frontend::debug() to inspect the rendered asset tags:
dd(Frontend::css('app')); // Debug the output
Log Asset Resolution Enable debug mode in the config to log asset paths:
'debug' => env('APP_DEBUG', false),
Clear Cached Config
If changes to config/frontend.php don’t take effect:
php artisan config:clear
Custom Asset Types Extend the bundle to support additional asset types (e.g., fonts, SVGs):
// In a service provider
Frontend::extend('font', function ($asset, array $options) {
return '<link href="' . asset($asset['path']) . '" rel="preload" as="font">';
});
Asset Filters Add middleware-like filters for asset tags:
Frontend::filter('css', function ($tag) {
return str_replace('rel="stylesheet"', 'rel="preload"', $tag);
});
Dynamic Asset Sources Override the asset resolver for dynamic sources (e.g., database-driven assets):
Frontend::resolver(function ($type, $name) {
return DB::table('frontend_assets')->where('name', $name)->first()->path;
});
Event Listeners Listen for asset compilation events (e.g., post-build hooks for Vite/Mix):
// In EventServiceProvider
FrontendBundle::assetCompiled(function ($asset) {
// Log or process compiled assets
});
How can I help you explore Laravel packages today?