betsol/assets-referencer-bundle
Installation
Run composer require betsol/assets-referencer-bundle ~1.0 in your Laravel project (note: this is a Symfony bundle, but can be adapted for Laravel via bridge packages like symfony/bundle or kris/laravel-symfony-bundle).
If using Laravel directly, consider wrapping this in a custom service provider for compatibility.
Configuration
Add the bundle to your config/app.php under providers (if using a bridge) or manually register it in a custom provider:
$this->register(new \Betsol\Bundle\AssetsReferencerBundle\AssetsReferencerBundle());
Publish the config (if available) or define it in config/services.php:
'assets_referencer' => [
'base_url' => env('ASSETS_BASE_URL', 'http://static.example.com/'),
],
First Use Case
In a Twig template (or a Laravel Blade template via a custom directive), use the asset_reference function:
<link rel="stylesheet" href="{{ asset_reference('css/app.css') }}">
Ensure your template engine supports Twig functions (e.g., via twig/laravel or a custom bridge).
Centralized Asset Management
Use asset_reference for all static assets (CSS, JS, images) to enforce consistency. Example:
{% for file in ['app.js', 'vendor.js', 'styles.css'] %}
<script src="{{ asset_reference('js/' ~ file) }}"></script>
{% endfor %}
Dynamic Base URLs
Override the base_url in config for different environments (e.g., dev, prod):
// config/services.php
'assets_referencer' => [
'base_url' => env('APP_ENV') === 'prod'
? 'https://cdn.example.com'
: 'http://localhost:8080/assets',
],
Integration with Laravel Mix/Vite Combine with Laravel’s asset pipelines. Example:
// vite.config.js
export default {
build: {
outDir: 'public/assets', // Output to a subdirectory
},
};
Then reference via:
{{ asset_reference('assets/app.js') }}
Twig in Laravel Blade Create a custom Blade directive to bridge Twig functions:
// AppServiceProvider.php
Blade::directive('asset', function ($path) {
return "<?php echo app('assets_referencer')->reference(" . $path . "); ?>";
});
Usage:
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
{{ asset_reference('js/app.js?v=' ~ hash('md5', 'app.js')) }}
{% if app.environment == 'dev' %}
{{ asset_reference('js/dev.js') }}
{% else %}
{{ asset_reference('js/prod.js') }}
{% endif %}
Symfony Dependency The bundle is Symfony-specific. For Laravel, you’ll need to:
kris/laravel-symfony-bundle).mix-manifest.json).Twig Requirement The bundle assumes Twig templates. For Blade-only projects:
// app/Helpers/AssetHelper.php
class AssetHelper {
public static function reference($path) {
return config('assets_referencer.base_url') . ltrim($path, '/');
}
}
Usage in Blade:
{{ AssetHelper::reference('css/app.css') }}
Path Handling Quirks
public folder. Prepend paths with / or configure base_url to include it:
base_url: http://example.com/public/
./images/logo.png) may break. Use absolute paths from the public root.Configuration Overrides
The base_url config is global. To support multiple domains:
base_url is correct and paths are absolute (e.g., css/app.css not ./css/app.css).twig/laravel or a custom provider).php artisan cache:clear) and Twig’s cache if applicable.Custom Functions
Extend the bundle by adding more Twig functions (e.g., asset_versioned):
// Extend the bundle's Twig loader
$twig->addFunction(new \Twig\TwigFunction('asset_versioned', function ($path) {
return asset_reference($path) . '?v=' . filemtime(public_path($path));
}));
Asset Processing Hook into the path resolution to add logic (e.g., minification checks):
// Override the bundle's resolver
$resolver = $bundle->getResolver();
$resolver->addFilter(function ($path) {
return str_replace('.css', '.min.css', $path);
});
Laravel Service Provider Bridge Create a Laravel-specific provider to abstract Symfony dependencies:
// AppServiceProvider.php
public function register() {
$this->app->singleton('assets_referencer', function () {
$container = new \Symfony\Component\DependencyInjection\Container();
$container->set('assets_referencer.base_url', config('assets_referencer.base_url'));
return new \Betsol\Bundle\AssetsReferencerBundle\AssetsReferencer($container);
});
}
Testing
Mock the asset_reference function in PHPUnit:
Twig::createNumberFunction()->expects($this)->once()->with('css/app.css')->willReturn('http://static.example.com/css/app.css');
How can I help you explore Laravel packages today?