Skip to content
Rumen Damyanov edited this page Jul 29, 2025 · 6 revisions

Examples Index

Welcome to the php-assets examples! This collection provides comprehensive guides for using the php-assets package in various scenarios.

Getting Started

If you're new to php-assets, start with these examples:

  1. Basic Usage - Learn the fundamentals of adding and outputting assets
  2. Asset Groups and Ordering - Organize assets into groups and control loading order

Core Features

  1. Cache Busting - Implement cache busting for updated assets
  2. Environment Detection - Automatic environment-aware behavior

Advanced Topics

  1. CDN Integration - Use Content Delivery Networks for better performance
  2. Framework Integration - Integrate with Laravel, Symfony, and other frameworks
  3. Advanced Features - Wildcard loading, inline scripts, and performance optimization

Quick Reference

Basic Usage

use Rumenx\Assets\Asset;

// Add assets
Asset::add('css/app.css');
Asset::add('js/app.js');

// Output assets
echo Asset::css();
echo Asset::js();

With Environment and CDN

// 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');

Asset Groups

// 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

Package Philosophy

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

Common Use Cases

Laravel Application

// In a controller or middleware
Asset::add('css/app.css');
Asset::add('js/app.js');

// In Blade templates
{!! Asset::css() !!}
{!! Asset::js() !!}

Symfony Application

// 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 }}

Plain PHP

// Configuration
Asset::setDomain('https://cdn.example.com/');
Asset::add('css/app.css');
Asset::add('js/app.js');

// In templates
<?= Asset::css() ?>
<?= Asset::js() ?>

Best Practices

  1. Environment-specific configuration: Use different settings for development and production
  2. Group organization: Organize assets logically (header, footer, admin, etc.)
  3. CDN usage: Use CDNs in production for better performance
  4. Cache busting: Implement proper cache busting strategy
  5. Asset ordering: Pay attention to dependency order (jQuery before Bootstrap, etc.)

Migration Guide

From Other Asset Managers

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();

From Framework-Specific Solutions

  • Laravel Mix: Replace mix() calls with Asset::add()
  • Symfony AssetMapper: Replace asset() calls with Asset::add()
  • Custom solutions: Replace with standardized API

Contributing

Found an issue with these examples or want to add more? Please:

  1. Check the main repository
  2. Open an issue or pull request
  3. Follow the contributing guidelines

Support


Next Steps: Start with Basic Usage if you're new to the package, or jump to any specific example that matches your needs.

Clone this wiki locally