fkr/cssurlrewrite-bundle
Symfony2/Assetic filter that rewrites relative url() paths in CSS so assets (images/fonts) resolve correctly after assets:install. Optionally rewrites only when the target file exists and can normalize/clean generated URLs.
Installation: Add the bundle via Composer:
composer require fkr/cssurlrewrite-bundle
Register the bundle in AppKernel.php:
new Fkr\CssURLRewriteBundle\FkrCssURLRewriteBundle(),
Configuration:
Add minimal config to app/config.yml:
fkr_css_url_rewrite:
rewrite_only_if_file_exists: true
clear_urls: true
First Use Case:
Place a CSS file in BundleName/Resources/public/css/ with relative paths (e.g., background-image: url(../img/logo.png)).
Use the filter in Twig:
{% stylesheets filter='css_url_rewrite'
'@BundleName/Resources/public/css/style.css'
%}
<link rel="stylesheet" href="{{ asset_url }}">
{% endstylesheets %}
Run php app/console assets:install to generate symlinks and test.
Standard CSS Path Resolution:
../img/logo.png) and let the filter resolve them to absolute paths like /bundles/bundlename/css/../img/logo.png./* Input */
background-image: url(../img/logo.png);
/* Output (after filter) */
background-image: url(../bundles/bundlename/css/../img/logo.png);
Cross-Bundle Asset References:
@BundleName notation:
/* Input */
background-image: url(@AcmeMediaBundle/img/logo.png);
/* Output */
background-image: url(../bundles/acmemediabundle/img/logo.png);
Path Normalization:
clear_urls: true to simplify complex paths (e.g., ../less/../img → ../img).Base64 Support:
background-image: url('data:image/png;base64,...');
Development Workflow:
assets:install --symlink for local development to avoid hardcoding paths.assetic:dump --watch for real-time updates.Deployment Workflow:
assets:install --env=prod to generate production-ready assets.app/cache/prod/...).Debugging Workflow:
filter='?css_url_rewrite') to isolate path issues.FkrCssURLRewriteBundle errors during assetic:dump.Assetic Filter Chaining:
Combine with other filters (e.g., yui_css, lessphp) for multi-step processing:
{% stylesheets filter='css_url_rewrite,yui_css'
'@BundleName/Resources/public/css/style.less'
%}
Environment-Specific Config:
Override settings per environment (e.g., disable rewrite_only_if_file_exists in dev):
# app/config_dev.yml
fkr_css_url_rewrite:
rewrite_only_if_file_exists: false
Custom Path Handling:
Extend the filter by subclassing Fkr\CssURLRewriteBundle\Filter\CssURLRewriteFilter and overriding rewriteUrl().
Assetic Dependency:
2.8.*) if using Symfony 3.Path Resolution Failures:
rewrite_only_if_file_exists: true, missing files will not be rewritten, potentially breaking CSS.rewrite_only_if_file_exists: false for development or pre-check paths.Base64 Limitations:
Assetic Dump Issues:
assetic:dump (fixed in later versions). Ensure you’re on v1.0.4.assetic:dump --no-debug if issues persist.Symfony 3+ Compatibility:
v1.0.1 and monitor for regressions.Log Filter Output: Enable debug mode to see rewritten URLs:
# app/config_dev.yml
fkr_css_url_rewrite:
debug: true
Inspect Compiled CSS:
Check app/cache/{env}/... for filtered CSS files to verify path changes.
Common Errors:
assets:install is run.url(../img/) without filename).AsseticBundle is installed and registered.Performance:
{% if app.environment == 'prod' %}
{% stylesheets '@BundleName/Resources/public/css/style.css' %}
{% else %}
{% stylesheets filter='css_url_rewrite' '@BundleName/Resources/public/css/style.css' %}
{% endif %}
Testing:
$filter = new \Fkr\CssURLRewriteBundle\Filter\CssURLRewriteFilter();
$result = $filter->filter('background-image: url(../img/logo.png)');
Extending the Filter:
rewriteUrl() to add custom logic (e.g., environment-specific paths):
class CustomCssURLRewriteFilter extends \Fkr\CssURLRewriteBundle\Filter\CssURLRewriteFilter {
protected function rewriteUrl($url) {
if (strpos($url, '@custom/') === 0) {
return str_replace('@custom/', '/custom-assets/', $url);
}
return parent::rewriteUrl($url);
}
}
Fallback for Missing Files:
rewrite_only_if_file_exists is true:
// In a custom filter
if (!file_exists($path) && $this->container->getParameter('kernel.environment') === 'dev') {
throw new \RuntimeException("CSS file not found: {$path}");
}
Legacy Code:
v1.0.1 and update dependencies:
composer require symfony/symfony:3.4.*
How can I help you explore Laravel packages today?