Install the Bundle
composer require sp/bower-bundle
Add to config/bundles.php:
Sp\BowerBundle\SpBowerBundle::class => ['all' => true],
Configure Bower
Edit config/packages/sp_bower.yaml (or create it):
sp_bower:
offline: false # Set to true if behind a proxy/firewall
components_dir: "%kernel.project_dir%/vendor/bower_components"
assets_dir: "%kernel.project_dir%/web/bower_components"
packages: [] # Define packages per environment if needed
Install Bower Packages
php bin/console sp:bower:install jquery bootstrap
This installs packages to vendor/bower_components and symlinks them to web/bower_components.
Use in Templates In Twig:
{{ asset('bower_components/jquery/dist/jquery.js') }}
Bundle-Specific Packages
Define packages per bundle in Resources/config/sp_bower.yaml:
sp_bower:
packages:
AcmeDemoBundle:
- jquery
- bootstrap
Install them via:
php bin/console sp:bower:install --bundle=AcmeDemoBundle
Asset Registration Automatically registers all installed Bower packages as Assetic resources. Use in Twig:
{% stylesheets
'bower_components/bootstrap/dist/css/bootstrap.css'
filter='cssrewrite'
%}
<link rel="stylesheet" href="{{ asset_url }}">
{% endstylesheets %}
Cache Management Warm the cache to generate Assetic bundles:
php bin/console cache:warmup
Or manually trigger Bower updates:
php bin/console sp:bower:update
Environment-Specific Config
Override sp_bower.yaml per environment (e.g., config/packages/dev/sp_bower.yaml):
sp_bower:
offline: true
packages:
AcmeDemoBundle:
- jquery@3.0.0
Deprecation Warning
Bower Version Mismatch
sp/bower-bundle ≥0.8.sp/bower-bundle ≤0.7 (see upgrade guide).Cache Issues
php bin/console cache:clear
php bin/console cache:warmup
sp_bower.yaml.Windows TTY Errors
--no-interactive flag to avoid TTY issues:
php bin/console sp:bower:install --no-interactive
Symlink Failures
web/bower_components is writable. Manually create symlinks if needed:
ln -s vendor/bower_components/web/bower_components
List Installed Packages
php bin/console sp:bower:list
Outputs all registered Assetic resources.
Check Bower Paths
Verify components_dir and assets_dir in sp_bower.yaml point to correct locations.
Offline Mode
Set offline: true if behind a proxy, but ensure vendor/bower_components is pre-populated.
Assetic Debugging
Enable Assetic debug mode in config/packages/dev/assetic.yaml:
assetic:
debug: true
Custom Commands
Extend Sp\BowerBundle\Command\BowerCommand for custom logic (e.g., post-install hooks).
Event Listeners
Subscribe to sp.bower.install or sp.bower.update events to run custom tasks:
// src/EventListener/BowerListener.php
public function onBowerInstall(InstallEvent $event) {
// Post-install logic (e.g., run Grunt tasks)
}
Override Asset Paths
Extend Sp\BowerBundle\Twig\Extension\BowerExtension to modify asset URLs dynamically.
CI/CD Integration
Use sp:bower:update in CI pipelines to ensure consistent asset versions:
# .github/workflows/deploy.yml
- run: php bin/console sp:bower:update --no-interactive
How can I help you explore Laravel packages today?