-
Notifications
You must be signed in to change notification settings - Fork 15
Home
Rumen Damyanov edited this page Jul 29, 2025
·
6 revisions
Welcome to the php-assets examples! This collection provides comprehensive guides for using the php-assets package in various scenarios.
If you're new to php-assets, start with these examples:
- Basic Usage - Learn the fundamentals of adding and outputting assets
- Asset Groups and Ordering - Organize assets into groups and control loading order
- Cache Busting - Implement cache busting for updated assets
- Environment Detection - Automatic environment-aware behavior
- CDN Integration - Use Content Delivery Networks for better performance
- Framework Integration - Integrate with Laravel, Symfony, and other frameworks
- Advanced Features - Wildcard loading, inline scripts, and performance optimization
use Rumenx\Assets\Asset;
// Add assets
Asset::add('css/app.css');
Asset::add('js/app.js');
// Output assets
echo Asset::css();
echo Asset::js();
// Production configuration
if (Asset::getEnvironment() === 'production') {
Asset::setDomain('https://cdn.example.com/');
Asset::setCachebuster('/path/to/assets.json');
}
Asset::add('css/app.css');
Asset::add('js/app.js');
// Add to specific groups
Asset::add('js/jquery.min.js', 'header');
Asset::add('js/app.js');
Asset::add('js/analytics.js', 'footer');
// Output by group
echo Asset::js('header'); // Header scripts
echo Asset::js(); // Default group
echo Asset::js('footer'); // Footer scripts
The php-assets package follows these principles:
- Framework-agnostic: Works with any PHP project
- Simple API: Easy to learn and use
- No magic: Predictable behavior
- Performance-focused: Built-in CDN and cache busting support
- Environment-aware: Automatic detection and optimization
// In a controller or middleware
Asset::add('css/app.css');
Asset::add('js/app.js');
// In Blade templates
{!! Asset::css() !!}
{!! Asset::js() !!}
// In a controller
Asset::add('css/app.css');
Asset::add('js/app.js');
// In Twig templates (with custom extension)
{{ asset_css()|raw }}
{{ asset_js()|raw }}
// Configuration
Asset::setDomain('https://cdn.example.com/');
Asset::add('css/app.css');
Asset::add('js/app.js');
// In templates
<?= Asset::css() ?>
<?= Asset::js() ?>
- Environment-specific configuration: Use different settings for development and production
- Group organization: Organize assets logically (header, footer, admin, etc.)
- CDN usage: Use CDNs in production for better performance
- Cache busting: Implement proper cache busting strategy
- Asset ordering: Pay attention to dependency order (jQuery before Bootstrap, etc.)
The php-assets package is designed to be a simple replacement for complex asset management systems:
// Instead of complex configurations
// Just use the simple API
Asset::add('css/app.css');
Asset::add('js/app.js');
echo Asset::css();
echo Asset::js();
-
Laravel Mix: Replace
mix()
calls withAsset::add()
-
Symfony AssetMapper: Replace
asset()
calls withAsset::add()
- Custom solutions: Replace with standardized API
Found an issue with these examples or want to add more? Please:
- Check the main repository
- Open an issue or pull request
- Follow the contributing guidelines
Next Steps: Start with Basic Usage if you're new to the package, or jump to any specific example that matches your needs.