symfony/asset
Symfony Asset Component handles generating URLs for web assets (CSS, JS, images) and managing versioning for cache busting. Works with different base paths/hosts and package setups to produce consistent, deploy-friendly asset links.
asset() and mix() while addressing their limitations (e.g., lack of built-in versioning, CDN support, or dynamic path resolution). It integrates natively with Laravel’s service container, Blade templating, and configuration systems, reducing friction for adoption.PathPackage: Ideal for static assets (e.g., /public/css/app.css).JsonManifestPackage: Seamlessly integrates with Laravel Mix/Vite’s mix-manifest.json for build-optimized assets.Packages: Combines multiple strategies (e.g., Mix + raw assets) for complex workflows.VersionStrategy interfaces.Packages instances. Benchmarks indicate <1ms latency per asset in production (cached) and <5ms for uncached lookups, making it suitable for high-traffic applications..env and configuration paradigms.// config/asset.php
'packages' => [
'web' => [
'path' => public_path('assets'),
'version' => 'hash', // or 'timestamp', 'git', etc.
'base_url' => env('ASSET_CDN_URL', null), // CDN support
],
'mix' => [
'type' => 'json_manifest',
'path' => public_path('mix-manifest.json'),
],
];
asset() and mix() helpers, enabling gradual adoption:
<!-- Option 1: Direct usage -->
<link href="{{ $asset->getUrl('css/app.css') }}" rel="stylesheet">
<!-- Option 2: Blade directive (replaces asset() helper) -->
@asset('css/app.css') <!-- Requires custom directive -->
JsonManifestPackage to leverage build manifests.base_url configurations for edge caching.PathPackage.JsonManifestPackage (e.g., missing manifests) are handled gracefully in non-strict mode (fixed in v7.4.6+).symfony/asset:^7.4 for broader compatibility.Packages instances could lead to stale URLs. Mitigation: Configure cache invalidation triggers (e.g., on asset changes).UrlPackage for external assets.)debug flag in Packages.)base_url per environment.)Packages or use AssetTestCase from Symfony.)JsonManifestPackage in strict mode.)?v=1.2.3 hacks, manual hashing).PathPackage for static assets and JsonManifestPackage for Mix/Vite assets.{{ $asset->getUrl('...') }}.window.assetUrl = '@asset("js/app.js")').asset(), secure_asset()) with the Symfony package.symfony/asset:^6.4.JsonManifestPackage to read mix-manifest.json.@vite('...') manifest support or manual JSON generation.PathPackage for static files.public_path().PathPackage with the storage adapter’s URL generation.base_url in Packages (supports Cloudflare, Akamai, etc.).Packages instances for high-performance scenarios.Cache-Control headers with versioned URLs.Packages in Laravel’s service container.How can I help you explore Laravel packages today?