devlabs91/cssurlrewrite-bundle
Installation Add the bundle via Composer:
composer require fkr/cssurlrewrite-bundle
Or manually via Git submodule/deps (if not using Composer).
Enable the Bundle
Register in app/AppKernel.php:
new Fkr\CssURLRewriteBundle\FkrCssURLRewriteBundle(),
Configure
Add minimal config in app/config.yml:
fkr_css_url_rewrite:
rewrite_only_if_file_exists: true # Default: true
clear_urls: true # Default: false
First Use Case Apply the filter to an Assetic CSS asset in a Twig template:
{% stylesheets
'bundles/yourbundle/css/style.css'
filter='cssurlrewrite'
%}
<link rel="stylesheet" href="{{ asset_url }}">
{% endstylesheets %}
Integrate with Assetic
Use the cssurlrewrite filter in Twig or PHP:
{% stylesheets 'path/to/styles.css' filter='cssurlrewrite' %}
Or programmatically:
$asset = $this->get('assetic.asset_manager')->getAsset('path/to/styles.css');
$asset->setTargetPath('path/to/output.css');
$asset->setFilter(new \Fkr\CssURLRewriteBundle\Filter\CssUrlRewriteFilter());
Dynamic URL Handling
../images/logo.png to absolute URLs (e.g., /bundles/yourbundle/images/logo.png).dev, test, and prod environments (leverages Symfony’s asset() helper).Chaining Filters
Combine with other Assetic filters (e.g., cssrewrite, yui_compressor):
{% stylesheets 'styles.css' filter='cssurlrewrite,yui_compressor' %}
Twig Extensions Use the bundle’s Twig extension for direct URL rewriting in templates:
{{ fkr_css_url_rewrite('relative/path/to/image.png') }}
Custom Base Paths Override the base path for specific assets via config:
fkr_css_url_rewrite:
base_path: /custom/base/path
Conditional Rewriting Disable rewriting for non-critical assets:
fkr_css_url_rewrite:
rewrite_only_if_file_exists: false # Rewrite even if file doesn’t exist
Debugging Mode
Enable clear_urls: false to preserve original paths for debugging:
fkr_css_url_rewrite:
clear_urls: false
Event Listeners Extend functionality via Symfony events (e.g., post-asset dump):
// src/Acme/EventListener/CssRewriteListener.php
class CssRewriteListener
{
public function onKernelRequest(GetResponseEvent $event)
{
if ($event->isMasterRequest()) {
// Custom logic here
}
}
}
File Existence Checks
rewrite_only_if_file_exists: true (default), missing files will not be rewritten.false if you need to rewrite paths regardless of file existence.Circular References
@import rules in CSS may cause infinite loops if paths are malformed.clear_urls: true to reset paths.Assetic Cache Invalidation
php app/console assetic:dump --env=prod --no-debug
Environment-Specific Paths
asset() helper is misconfigured (e.g., wrong public_path).asset() output matches your expected paths.Log Rewritten URLs
Enable debug mode in config.yml:
fkr_css_url_rewrite:
debug: true # Logs rewritten paths to Symfony’s log channel
Inspect Filter Output Dump the filtered content to verify changes:
$filter = new \Fkr\CssURLRewriteBundle\Filter\CssUrlRewriteFilter();
$content = $filter->filter($asset->getContent());
var_dump($content); // Check rewritten paths
Test Locally First
dev environment to test rewrites before deploying to prod.asset() in Twig for testing:
{{ asset('path/to/file.css', { debug: true }) }}
Custom URL Strategies Override the default URL generator:
// src/Fkr/CssURLRewriteBundle/Filter/CustomUrlRewriteFilter.php
class CustomUrlRewriteFilter extends \Fkr\CssURLRewriteBundle\Filter\CssUrlRewriteFilter
{
protected function generateUrl($path)
{
return '/custom/' . parent::generateUrl($path);
}
}
Register in services.yml:
services:
custom.cssurlrewrite.filter:
class: Fkr\CssURLRewriteBundle\Filter\CustomUrlRewriteFilter
tags:
- { name: assetic.filter, alias: custom_cssurlrewrite }
Pre/Post-Processing
Hook into the filter’s filter() method to add logic:
$content = $this->preProcess($content);
$content = parent::filter($content);
$content = $this->postProcess($content);
return $content;
Exclude Specific Files
Use Assetic’s exclude option in Twig:
{% stylesheets 'styles.css' filter='cssurlrewrite' exclude='vendor.css' %}
How can I help you explore Laravel packages today?