Framework-agnostic PHP package to manage frontend assets in the backend. Works with plain PHP, Laravel, and Symfony (via adapters).
- Add, order, and output CSS, LESS, and JS assets from PHP
- Cache busting (file or function based)
- Environment and domain support
- Laravel and Symfony integration via adapters
- 100% test coverage, static analysis, and CI
composer require rumenx/php-assets
use Rumenx\Assets\Asset;
// Add assets
Asset::add('style.css');
Asset::add('theme.less');
Asset::add('app.js');
Asset::add(['extra.js', 'extra2.js'], 'footer');
// Add inline style or script
Asset::addStyle('body { background: #fafafa; }');
Asset::addScript('console.log("Hello!");');
// Output in your template
Asset::css(); // <link rel="stylesheet" ...>
Asset::less(); // <link rel="stylesheet/less" ...>
Asset::js(); // <script src=...></script>
Asset::styles(); // <style>...</style>
Asset::scripts(); // <script>...</script>
// Use cachebuster (file-based)
Asset::setCachebuster(__DIR__.'/cache.json');
// Use cachebuster (function-based)
Asset::setCacheBusterGeneratorFunction(function($file) {
return md5($file);
});
// Custom domain or prefix
Asset::setDomain('https://cdn.example.com/');
Asset::setPrefix('X-');
-
Register the service provider in
config/app.php
:Rumenx\Assets\Laravel\AssetServiceProvider::class,
-
Use the Asset class anywhere in your app:
use Rumenx\Assets\Asset; Asset::add('main.css'); Asset::add('main.js'); // In your Blade template {!! Asset::css() !!} {!! Asset::js() !!}
-
(Optional) Bindings are available via the Laravel container:
$assets = app('assets'); $assets::add('custom.js');
-
Register the bundle in your Symfony app:
// config/bundles.php return [ Rumenx\Assets\Symfony\AssetBundle::class => ['all' => true], ];
-
Use the Asset class in your controllers or templates:
use Rumenx\Assets\Asset; Asset::add('main.css'); Asset::add('main.js'); // In a Twig template dump(Asset::css()); dump(Asset::js());
- Add assets to specific locations:
Asset::add('file.js', 'header');
// Add JS to headerAsset::addFirst('file.js');
// Add as first assetAsset::addBefore('new.js', 'old.js');
// Insert before anotherAsset::addAfter('new.js', 'old.js');
// Insert after another
- Environment detection:
Asset::$envResolver = fn() => app()->environment();
- Custom URL generator:
Asset::$urlGenerator = fn($file, $secure) => asset($file, $secure);
composer test
composer analyze
- GitHub Actions for tests, static analysis, and Codecov coverage reporting.
This project is licensed under the MIT License.