symfony/asset
Symfony Asset Component generates URLs for web assets (CSS, JS, images) with built-in versioning for cache busting. Supports base paths, packages, and CDNs to keep asset links consistent across environments and deployments.
symfony/asset package excels at server-side asset URL generation and versioning, addressing gaps in Laravel’s built-in asset() helper (e.g., no native fingerprinting or CDN support). It integrates seamlessly with Laravel’s service container, Blade templates, and asset pipelines (e.g., Laravel Mix/Vite).Packages interface allows for flexible configuration (e.g., PathPackage, JsonManifestPackage for Mix/Vite compatibility). This enables granular adoption—e.g., using it only for non-bundled assets while preserving Laravel’s native helpers for others.app.abc123.css) and environment-aware versioning (e.g., disabling hashes in dev). Critical for high-traffic apps or asset-heavy SPAs/MPAs.// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton(\Symfony\Component\Asset\Packages::class, fn() =>
new \Symfony\Component\Asset\PathPackage(public_path(), true) // Enable versioning
);
}
{{ asset('...') }} with:
@inject('assets', 'Symfony\Component\Asset\Packages')
<link href="{{ $assets->getUrl('css/app.css') }}" rel="stylesheet">
JsonManifestPackage to defer to Mix/Vite’s manifest:
$package = new \Symfony\Component\Asset\JsonManifestPackage(
public_path('mix-manifest.json'),
'https://cdn.example.com'
);
public function handle($request, Closure $next)
{
$response = $next($request);
$response->setContent(
preg_replace(
'/href="(\/assets\/[^"]+)"/',
'href="' . $this->assets->getUrl('$1') . '"',
$response->getContent()
)
);
return $response;
}
symfony/asset generate hashes for the same files, cache inconsistencies may arise. Mitigation:
symfony/asset only for non-Mix assets (e.g., raw public/ files).JsonManifestPackage to respect Mix’s hashes:
$package = new JsonManifestPackage(
public_path('mix-manifest.json'),
null, // No base URL (use Mix’s hashes)
true // Strict mode
);
base_urls, signed URLs or dynamic subdomains may require custom logic (e.g., middleware to rewrite URLs at runtime).<img src="/img/logo.png">) will break if not migrated to dynamic generation. Use regex search/replace or static analysis tools (e.g., PHPStan) to audit templates.asset(), mix()), or only specific cases (e.g., raw public/ files)?symfony/asset both generate hashes?Packages instance).@inject directives)?Packages as a singleton or context-bound instance.@inject or service binding for dynamic asset URLs.JsonManifestPackage.mix-manifest.json via JsonManifestPackage.base_urls for asset delivery (e.g., https://cdn.example.com).public/, S3, or custom paths.| Phase | Action | Tools/Examples |
|---|---|---|
| Assessment | Audit asset usage: Identify hardcoded paths, Mix/Vite dependencies. | grep -r "href=\"/assets/" app/ or PHPStan rules. |
| Pilot Integration | Replace non-Mix assets (e.g., raw public/ files) with PathPackage. |
new PathPackage(public_path(), true) in a service provider. |
| Blade Migration | Update templates to use @inject or service binding. |
@inject('assets', 'Symfony\Component\Asset\Packages') |
| Mix/Vite Sync | Configure JsonManifestPackage for bundled assets. |
new JsonManifestPackage(public_path('mix-manifest.json')) |
| Global Rewrite | Add middleware for legacy asset paths (optional). | Regex-based URL rewriting middleware. |
| Testing | Validate cache-busting, CDN delivery, and dev/prod behavior. | Load tests, cache headers inspection (e.g., curl -I). |
| Rollout | Deploy in stages (e.g., non-critical routes first). | Feature flags or environment-based toggles. |
JsonManifestPackage.PathPackage for raw asset directories.Cache-Control: max-age=31536000, immutable) work with fingerprinting.public/img/, public/css/) using PathPackage.JsonManifestPackage for fingerprinted assets.How can I help you explore Laravel packages today?