Installation
composer require 21torr/assets
Ensure compatibility with your firefly version (see README requirements).
Configuration Publish the config file:
php artisan vendor:publish --provider="21TORR\AssetsBundle\AssetsBundle" --tag=config
Update config/assets.php with your storage paths (e.g., public, private).
First Use Case Upload an asset via the Firefly UI (if integrated) or manually via:
use 21TORR\AssetsBundle\Model\Asset;
$asset = new Asset();
$asset->setFile('path/to/file.jpg');
$asset->save();
Access the URL via:
$asset->getUrl(); // e.g., `/assets/123/file.jpg`
Key Directories
config/assets.php: Core settings (paths, CDN, etc.).src/21TORR/AssetsBundle/: Core logic (models, services).Resources/config/: Default configurations.Asset Uploads
Asset model to handle files:
$asset = new Asset();
$asset->setFile($request->file('image'));
$asset->setMimeType($request->file('image')->getMimeType());
$asset->save();
routing.yml:
assets_upload:
path: /assets/upload
defaults: { _controller: AssetsBundle:Asset:upload }
Asset URLs and Paths
$url = $this->get('assets')->generateUrl($asset);
// or via Twig:
{{ asset(asset.id) }}
config/assets.php:
'paths' => [
'public' => 'uploads/%year%/%month%/%day%',
],
Asset Processing
Asset component for versioned URLs:
$this->get('assets')->getUrl($asset, ['version' => '1.0']);
AssetMapper for cache-busting:
<img src="{{ asset_url(asset.id) }}">
Batch Operations
php artisan assets:import path/to/directory
$assets = Asset::where('mime_type', 'like', 'image/%')->get();
// Example: Listen for Firefly entity events
Firefly\Event\EventDispatcher::addListener(
'firefly.entity.created',
function ($entity) {
// Auto-assign assets to entities
}
);
AssetStorage service to support S3 or other backends:
// config/services.yaml
assets.storage:
class: App\Service\CustomAssetStorage
arguments: ['@assets.storage.adapter']
Archived Package
symfony/asset for new projects.Firefly Dependency
composer.json for firefly version conflicts.Path Configuration
paths.public in config/assets.php breaks URL generation.storage/app/public/assets).Mime Type Handling
setMimeType(); rely on Symfony’s MimeTypeGuesser for auto-detection:
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
$guesser = new MimeTypeGuesser();
$asset->setMimeType($guesser->guess($file->getPathname()));
Caching Issues
php artisan cache:clear
asset_version in Twig to bust caches:
<img src="{{ path('assets_url', { id: asset.id, version: '1.0' }) }}">
storage/logs/laravel.log for file system errors. Verify paths.public exists and is writable.Asset model events (saving, saved) aren’t interfering with path generation.// config/firefly.php
'assets' => false,
Custom Asset Models
Extend 21TORR\AssetsBundle\Model\Asset for domain-specific logic:
namespace App\Model;
use 21TORR\AssetsBundle\Model\Asset as BaseAsset;
class ProductAsset extends BaseAsset
{
public function getProduct()
{
return $this->belongsTo(Product::class);
}
}
Storage Adapters Override the default storage adapter (e.g., for S3):
// src/Service/CustomStorageAdapter.php
namespace App\Service;
use 21TORR\AssetsBundle\Storage\AdapterInterface;
class CustomStorageAdapter implements AdapterInterface
{
public function save($file, $path)
{
// Custom logic (e.g., S3 upload)
}
}
Register in config/services.yaml:
assets.storage.adapter: App\Service\CustomStorageAdapter
Events
Listen for asset events (e.g., asset.pre_save):
// EventListener.php
namespace App\Listener;
use 21TORR\AssetsBundle\Event\AssetEvent;
class AssetListener
{
public function onPreSave(AssetEvent $event)
{
$event->getAsset()->setUserId(auth()->id());
}
}
Register in services.yaml:
services:
App\Listener\AssetListener:
tags:
- { name: kernel.event_listener, event: asset.pre_save, method: onPreSave }
'debug' => false in config/assets.php to disable cache-busting in production.cdn_url in config/assets.php for offloaded assets:
'cdn' => [
'enabled' => true,
'url' => 'https://cdn.example.com/assets/%id%',
],
Asset::chunk() for large datasets to avoid memory issues:
Asset::chunk(100, function ($assets) {
foreach ($assets as $asset) {
// Process asset
}
});
How can I help you explore Laravel packages today?