### basset:install Output
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/console-commands.md
Example output after running the basset:install command.
```bash
Installing Basset
Adding post-install-cmd to composer.json DONE
Checking Backpack Basset installation
Initializing basset check DONE
Checking cache storage DONE
Fetching a basset DONE
Done
```
--------------------------------
### basset:install Typical Usage
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/console-commands.md
Examples of how to use the basset:install command.
```bash
# Initial installation
composer require backpack/basset
php artisan basset:install
# If using public_basset disk (to add to .gitignore)
php artisan basset:install --git-ignore
```
--------------------------------
### prepareAttributes() Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/api-reference/file-output.md
Example of how attributes are merged and prepared.
```php
// Config has: ['data-cfasync' => 'false']
// Asset has: ['defer' => true, 'integrity' => 'sha384-...']
// Result: ' data-cfasync="false" defer integrity="sha384-..."'
```
--------------------------------
### Development Configuration Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/configuration.md
Example configuration settings for Basset in a development environment.
```php
// config/backpack/basset.php
return [
'dev_mode' => true,
'verify_ssl_certificate' => false,
'disk' => 'basset',
'cache_map' => true,
'relative_paths' => true,
];
```
--------------------------------
### Production Configuration Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/configuration.md
Example configuration settings for Basset in a production environment.
```php
return [
'dev_mode' => false,
'verify_ssl_certificate' => true,
'disk' => 'basset',
'cache_map' => true,
'relative_paths' => true,
];
```
--------------------------------
### write() Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/api-reference/file-output.md
Example usage of the write() method.
```php
$entry = (new CacheEntry())
->assetName('jquery')
->assetPath('https://code.jquery.com/jquery-3.6.0.min.js');
$output = app(FileOutput::class);
$output->write($entry);
// Outputs:
```
--------------------------------
### disk Configuration Examples
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/configuration.md
Examples of setting the 'disk' configuration option via environment variables.
```bash
# Use public disk (default)
BASSET_DISK=basset
# Use public_basset disk
BASSET_DISK=public_basset
# Use custom S3 disk (configure in filesystems.php first)
BASSET_DISK=s3
```
--------------------------------
### path Configuration Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/configuration.md
Example of setting the 'path' configuration option via environment variables.
```bash
# .env
BASSET_PATH=assets # Assets stored at {disk}/assets/
```
--------------------------------
### basset:install Command Syntax
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/console-commands.md
The basic syntax for the basset:install command with options.
```bash
php artisan basset:install [--no-check] [--git-ignore]
```
--------------------------------
### assetPath() Example (Development)
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/api-reference/file-output.md
Example of assetPath() in development mode.
```php
$output = app(FileOutput::class);
// Development (dev_mode=true)
$output->assetPath('basset/jquery.js');
// Returns: '/storage/basset/jquery.js'
```
--------------------------------
### Shell/Artisan Command Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/INDEX.md
Example of running the basset:cache Artisan command.
```bash
# Shell/Artisan commands
php artisan basset:cache
```
--------------------------------
### Loading Assets with Helper Function
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/README.md
Example of using the global `basset()` helper function to get an asset URL.
```php
{{ basset('https://cdn.example.com/file.js') }} {{-- Return URL only --}}
```
--------------------------------
### Getting Asset URLs
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/README.md
Example of retrieving the URL for a loaded asset.
```php
$url = Basset::getUrl('jquery'); // /storage/basset/jquery-abc123.js?xyz
```
--------------------------------
### assetPath() Example (Production)
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/api-reference/file-output.md
Example of assetPath() in production mode.
```php
$output = app(FileOutput::class);
// Production (dev_mode=false)
$output->assetPath('basset/jquery.js');
// Returns: '/storage/basset/jquery.js?abc123def456'
```
--------------------------------
### loadDisk() configuration example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/service-provider.md
This configuration snippet shows how the primary 'basset' disk is configured if not already defined.
```php
'filesystems.disks.basset' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => url('').'/storage',
'visibility' => 'public',
'throw' => false,
]
```
--------------------------------
### Example: assetAttributes()
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/api-reference/cache-entry.md
Example of setting HTML attributes for an asset.
```php
$entry->assetAttributes([
'defer' => true,
'integrity' => 'sha384-',
'crossorigin' => 'anonymous',
]);
```
--------------------------------
### Nonce Output Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/configuration.md
Example of how a script tag will appear with the nonce attribute configured.
```html
```
--------------------------------
### Example: assetName()
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/api-reference/cache-entry.md
Example of chaining `assetName()` and `assetPath()` setters.
```php
(new CacheEntry())
->assetName('jquery')
->assetPath('https://code.jquery.com/jquery-3.6.0.min.js');
```
--------------------------------
### Verify Installation Command
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/INDEX.md
Command to verify Basset installation.
```bash
php artisan basset:check
```
--------------------------------
### PHP Code Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/INDEX.md
Example of loading an asset using the Basset facade in PHP.
```php
// PHP code
Basset::basset('file.js');
```
--------------------------------
### basset:fresh Output Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/console-commands.md
Example output from the basset:fresh command, showing the clearing and caching process.
```bash
Clearing basset 'storage/app/public/basset'
Done
Looking for bassets under the following directories:
- resources/views (120 blade files)
Found 42 bassets in 165 blade files. Caching:
... (progress bar)
Successfully internalized: 42
```
--------------------------------
### dev_mode Configuration Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/configuration.md
Example of how to set the 'dev_mode' configuration option via environment variables.
```bash
# .env
BASSET_DEV_MODE=true # or false
```
--------------------------------
### Comparison with Facade examples
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/api-reference/helpers.md
Examples illustrating the differences in usage and output between the basset() function, Basset facade, and @basset directive.
```blade
{{-- Using helper for custom HTML --}}
{{-- Using facade for auto-rendered tag --}}
@basset('jquery') {{-- Renders:
@endif
```
--------------------------------
### Configuration Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/api-reference/asset-path-manager.md
Example of how the 'path' configuration for Basset can be set, using an environment variable.
```php
return [
'path' => env('BASSET_PATH', 'basset'),
];
```
--------------------------------
### basset:list-named Filtering Examples
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/console-commands.md
Examples demonstrating how to filter the output of the basset:list-named command.
```bash
# Show only jQuery-related assets
php artisan basset:list-named --filter=jquery
# Show only assets from cdn.example.com
php artisan basset:list-named --filter="cdn.example.com"
# Show CRUD-related assets
php artisan basset:list-named --filter="crud"
```
--------------------------------
### Example: assetPath()
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/api-reference/cache-entry.md
Examples of using `assetPath()` with a local file path and a CDN URL.
```php
$entry->assetPath(storage_path('uploads/custom.js'));
// or
$entry->assetPath('https://cdn.example.com/library.js');
```
--------------------------------
### Composer Installation
Source: https://github.com/laravel-backpack/basset/blob/main/readme.md
Commands to install the Basset package using Composer.
```bash
composer require backpack/basset
php artisan basset:install
```
--------------------------------
### loadPublicDisk() configuration example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/service-provider.md
This configuration snippet shows how the 'public_basset' disk is configured for use with the public/ directory.
```php
'filesystems.disks.public_basset' => [
'driver' => 'local',
'root' => public_path(),
'url' => url(''),
'visibility' => 'public',
'throw' => false,
]
```
--------------------------------
### Complete Example: Monitoring Asset Internalization
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/events.md
A complete example demonstrating how to set up a listener to monitor Basset asset internalization events.
```php
// app/Listeners/BassetMonitor.php
namespace App\Listeners;
use Backpack\Basset\Events\BassetCachedEvent;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
class BassetMonitor
{
public function handle(BassetCachedEvent $event): void
{
$isRemote = Str::isUrl($event->asset);
$type = $isRemote ? 'remote' : 'local';
Log::channel('assets')->info("Asset internalized [{$type}]", [
'asset' => $event->asset,
'timestamp' => now(),
]);
// Track metrics
if (app()->bound('metrics')) {
app('metrics')
->counter('basset.assets.internalized')
->increment();
}
}
}
// app/Providers/EventServiceProvider.php
class EventServiceProvider extends ServiceProvider
{
protected $listen = [
BassetCachedEvent::class => [
App\Listeners\BassetMonitor::class,
],
];
}
```
--------------------------------
### basset:clear Output Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/console-commands.md
Example output from the basset:clear command.
```bash
Clearing basset 'storage/app/public/basset'
Done
```
--------------------------------
### basset:cache Output Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/console-commands.md
Example output from the basset:cache command, showing progress and results.
```bash
Looking for bassets under the following directories:
- resources/views (120 blade files)
- resources/components (45 blade files)
Found 42 bassets in 165 blade files. Caching:
█████████████████████████████░░░░░░░░░░░░░░░░░░ 42/42
Successfully internalized: 40
Could not internalize: 2
- https://broken-cdn.example.com/lib.js (failed)
- non-existent-file.js (not found)
Completed in 3.24 seconds
```
--------------------------------
### Configuration Example: EventServiceProvider
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/events.md
Example of configuring multiple listeners for BassetCachedEvent in EventServiceProvider.
```php
protected $listen = [
BassetCachedEvent::class => [
'App\Listeners\LogAssetCached',
'App\Listeners\InvalidateCDNCache',
'App\Listeners\TrackAssetPerformance',
],
];
```
--------------------------------
### verify_ssl_certificate Configuration Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/configuration.md
Example of how to set the 'verify_ssl_certificate' configuration option via environment variables.
```bash
# .env
BASSET_VERIFY_SSL_CERTIFICATE=false # For development with self-signed certs
```
--------------------------------
### generateHash() Usage Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/interfaces-and-contracts.md
Example of how to use the generateHash() method.
```php
$hash = $hashManager->generateHash(file_get_contents('script.js'));
// Returns: 'a1b2c3d4' or similar
```
--------------------------------
### injectToBlock() Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/api-reference/file-output.md
Example of injecting script attributes into inline code.
```php
// Config: ['data-cfasync' => 'false']
// Input: ''
// Output: ''
```
--------------------------------
### Example Configuration
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/api-reference/file-output.md
Demonstrates how to configure FileOutput in Laravel Backpack.
```php
// config/backpack/basset.php
return [
'nonce' => env('CSP_NONCE'),
'relative_paths' => true,
'dev_mode' => env('APP_ENV') === 'local',
'script_attributes' => [
'data-cfasync' => 'false', // Cloudflare Rocket Loader opt-out
],
];
```
--------------------------------
### Custom Implementation Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/interfaces-and-contracts.md
Example of how to provide a custom implementation by registering it in a service provider.
```php
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton(
AssetPathManagerInterface::class,
CustomAssetPathManager::class
);
}
```
--------------------------------
### Nonce Configuration Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/configuration.md
Configuration for adding a Content Security Policy (CSP) nonce to script tags, with an example of how to set it dynamically.
```php
'nonce' => env('CSP_NONCE'),
```
```php
// In middleware or request handler
$nonce = Str::random(16);
request()->attributes->set('csp_nonce', $nonce);
// Then in config (or use a callable)
'nonce' => request('csp_nonce'),
```
--------------------------------
### getBasePath() Usage Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/interfaces-and-contracts.md
Example of how to use the getBasePath() method.
```php
$pathManager = app(AssetPathManagerInterface::class);
$basePath = $pathManager->getBasePath(); // 'basset/'
```
--------------------------------
### Check Basset Installation
Source: https://github.com/laravel-backpack/basset/blob/main/readme.md
Command to verify that Basset is installed and configured correctly.
```bash
# recommended - this will tell you if anything is wrong & what to do:
php artisan basset:check
```
--------------------------------
### Dependency Injection Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/interfaces-and-contracts.md
Example of type-hinting the AssetPathManagerInterface for dependency injection.
```php
public function myFunction(AssetPathManagerInterface $pathManager) {
$diskPath = $pathManager->getPathOnDisk('file.js');
}
```
--------------------------------
### getCleanPath() Usage Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/interfaces-and-contracts.md
Example of how to use the getCleanPath() method.
```php
$clean = $pathManager->getCleanPath('https://example.com/file.js?v=1');
// Returns: 'file.js'
```
--------------------------------
### Configuration Example: Attributes (Laravel 11+)
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/events.md
Example of configuring a listener using attributes for Laravel 11+.
```php
#[AsListener]
class LogAssetCached
{
public function handle(BassetCachedEvent $event): void
{
Log::info('Asset cached: ' . $event->asset);
}
}
```
--------------------------------
### Async Processing Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/events.md
Example of dispatching a listener in a queued job for asynchronous processing.
```php
class LogAssetCached
{
public function handle(BassetCachedEvent $event):
{
LogAssetJob::dispatch($event->asset);
}
}
```
--------------------------------
### basset() Method Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/api-reference/basset-manager.md
Examples of internalizing a single asset using the basset() method via Facade, helper, or programmatically.
```blade
// Via Facade in Blade
@basset('https://cdn.example.com/library.js')
```
```php
// Or via helper
{{ basset('https://cdn.example.com/library.css') }}
```
```php
// Or programmatically
Basset::basset(storage_path('uploads/custom.js'), true, ['async' => true]);
```
--------------------------------
### isLocal() Usage Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/interfaces-and-contracts.md
Example of how to use the isLocal() method.
```php
if ($pathManager->isLocal($path)) {
$content = file_get_contents($path);
}
```
--------------------------------
### Event Listener Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/README.md
Shows how to hook into asset internalization by listening for the BassetCachedEvent.
```php
use Backpack\Basset\Events\BassetCachedEvent;
class EventServiceProvider extends ServiceProvider
{
protected $listen = [
BassetCachedEvent::class => [
MyListener::class,
],
];
}
// Event Properties:
// - $asset — The asset path/URL being internalized
```
--------------------------------
### Loading Assets with Facade
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/README.md
Example of using the Basset Facade in PHP to load assets with options.
```php
Basset::basset('https://cdn.example.com/file.js', true, ['defer' => true]);
```
--------------------------------
### cache_map Configuration Example for Serverless
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/configuration.md
Example of disabling 'cache_map' for serverless environments like Vapor.
```bash
# .env (Vapor)
BASSET_DISK=s3
BASSET_CACHE_MAP=false
```
--------------------------------
### bassetDirectory() Method Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/api-reference/basset-manager.md
Example of copying an entire local directory into the basset directory using the bassetDirectory() method.
```blade
@bassetDirectory(resource_path('my-assets/'), 'my-assets')
@basset('my-assets/script.js')
```
--------------------------------
### Basset Helper Usage Examples
Source: https://github.com/laravel-backpack/basset/blob/main/readme.md
Examples showing how to use the basset() helper for various asset types and locations, including public, external URLs, and local non-public paths.
```blade
{{-- instead of --}}
{{-- you can do --}}
```
--------------------------------
### Accessing Services
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/service-provider.md
Examples of how to access Basset services via facade, service container, dependency injection, and interfaces.
```php
// Via facade
Basset::basset('https://example.com/file.js');
// Via service container
$manager = app('basset');
$manager->basset('https://example.com/file.js');
// Via dependency injection
public function __construct(BassetManager $basset) {
$this->basset = $basset;
}
// Via interfaces
public function __construct(
AssetPathManagerInterface $paths,
AssetHashManagerInterface $hasher
) {
// Use interfaces for loose coupling
}
```
--------------------------------
### basset:list-named Output
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/console-commands.md
Example output of the basset:list-named command listing registered assets.
```bash
Asset Key: jquery Asset Source: https://code.jquery.com/jquery-3.6.0.min.js
Asset Key: bootstrap Asset Source: https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js
Asset Key: crud.select2 Asset Source: https://cdn.example.com/select2/4.1.0/select2.min.js
...
```
--------------------------------
### Relative Paths Environment Variable Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/configuration.md
Example of setting the `BASSET_RELATIVE_PATHS` environment variable to control path usage.
```bash
# .env
BASSET_RELATIVE_PATHS=true # Uses /storage/basset/file.js
BASSET_RELATIVE_PATHS=false # Uses https://app.example.com/storage/basset/file.js
```
--------------------------------
### basset:cache Deployment Integration
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/console-commands.md
Example of how to integrate the basset:cache command into composer.json scripts for post-install and post-update commands.
```json
{
"scripts": {
"post-install-cmd": [
"php artisan storage:link",
"php artisan basset:cache"
],
"post-update-cmd": [
"php artisan storage:link",
"php artisan basset:cache"
]
}
}
```
--------------------------------
### bassetArchive() Method Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/api-reference/basset-manager.md
Example of extracting and internalizing an archive file using the bassetArchive() method.
```blade
@bassetArchive('https://github.com/author/pkg/archive/refs/tags/1.0.0.zip', 'pkg-1.0.0')
@basset('pkg-1.0.0/dist/plugin.min.js')
```
--------------------------------
### Script Attributes Example Output
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/configuration.md
Demonstrates the HTML output of script tags with and without additional attributes.
```html
```
--------------------------------
### Asset Overrides Implementation Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/configuration.md
An example of how to implement the `OverridesAssets` interface to map custom assets or override existing ones.
```php
namespace App\Providers;
use Backpack\Basset\OverridesAssets;
use Backpack\Basset\Facades\Basset;
class OverrideBassetAssets implements OverridesAssets
{
public function assets(): void
{
// Override package asset
Basset::map('crud.select2', 'https://my-cdn.com/select2.js');
// Add custom asset
Basset::map('my-custom-lib', 'https://example.com/lib.js');
}
}
```
--------------------------------
### Extending the Provider
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/service-provider.md
Example of creating a custom service provider to extend Basset functionality.
```php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Backpack\Basset\Facades\Basset;
class CustomBassetProvider extends ServiceProvider
{
public function boot(): void
{
// Register additional directives
// Extend BassetManager
// Add custom disk configuration
}
}
```
```php
'providers' => [
// ...
App\Providers\CustomBassetProvider::class,
],
```
--------------------------------
### bassetBlock() Method Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/api-reference/basset-manager.md
Example of internalizing a block of inline code (JavaScript or CSS) using the bassetBlock() method.
```blade
@bassetBlock('custom-init')
@endBassetBlock
```
--------------------------------
### basset() usage examples
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/api-reference/helpers.md
Examples of using the basset() helper function in Blade views for various asset types.
```blade
{{-- Named asset --}}
```
--------------------------------
### Path Handling Examples
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/api-reference/asset-path-manager.md
Illustrates various scenarios of path management using AssetPathManager.
```php
$manager = app(AssetPathManagerInterface::class);
// CDN URL
$manager->getPathOnDisk('https://cdn.example.com/lib/v2/app.min.js');
// Result: 'basset/lib/v2/app.min.js'
// Absolute file path
$manager->getPathOnDisk('/home/user/laravel/storage/my-file.css');
// Result: 'basset/my-file.css'
// Relative path with query string
$manager->getPathOnDisk('css/style.css?v=123');
// Result: 'basset/css/style.css'
// Malicious path (prevented)
$manager->getPathOnDisk('../../../etc/passwd');
// Result: 'basset/etc/passwd' (safely cleaned)
```
--------------------------------
### Usage Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/api-reference/cache-entry.md
Demonstrates how to create a CacheEntry, use it with BassetManager, serialize it for a cache map, and restore it from cache.
```php
use Backpack\Basset\Helpers\CacheEntry;
// Create from scratch
$entry = (new CacheEntry())
->assetName('jquery')
->assetPath('https://code.jquery.com/jquery-3.6.0.min.js')
->assetAttributes(['defer' => true]);
// Use with BassetManager
Basset::loadAsset($entry, true);
// Serialize for cache map
$map = json_encode($entry);
// Or restore from cache
$cached = json_decode(file_get_contents('.basset'), true);
$restored = CacheEntry::from($cached['jquery']);
```
--------------------------------
### LoadingTime Utility Usage
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/types.md
Example of how to access and use the LoadingTime utility class via Basset::loader to get total calls and loading time.
```php
$totalCalls = Basset::loader->getTotalCalls();
$totalTime = Basset::loader->getLoadingTime();
```
--------------------------------
### Docker Deployment Configuration
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/console-commands.md
Dockerfile instruction to install and cache assets during Docker image build.
```dockerfile
# In Dockerfile
RUN php artisan basset:install --no-check && \
php artisan basset:cache
```
--------------------------------
### Blade Template Code Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/INDEX.md
Example of loading an asset using the @basset directive in a Blade template.
```blade
{{-- Blade template code --}}
@basset('file.js')
```
--------------------------------
### basset:list-named Output with Filter
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/console-commands.md
Example output of the basset:list-named command when a filter is applied.
```bash
Asset Key: jquery Asset Source: https://code.jquery.com/jquery-3.6.0.min.js
```
--------------------------------
### Checking Loaded Assets
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/README.md
Examples of checking if an asset has already been loaded during the current request.
```php
if (Basset::isLoaded('jquery')) {
// jQuery has already been loaded this request
}
$loaded = Basset::loaded(); // Array of loaded asset names
```
--------------------------------
### basset:check Success Output
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/console-commands.md
Example output when the basset:check command runs successfully.
```bash
Checking Backpack Basset installation
Initializing basset check DONE
Checking cache storage DONE
Fetching a basset DONE
```
--------------------------------
### Basset Directive Usage Examples
Source: https://github.com/laravel-backpack/basset/blob/main/readme.md
Examples demonstrating the use of the @basset() directive for automatically generating HTML tags for various asset types like JS, CSS, images, and PDFs.
```blade
{{-- instead of --}}
@endonce
```
--------------------------------
### Internalizing Directories
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/README.md
Example of using the `@bassetDirectory` directive to internalize assets from a local directory.
```blade
@bassetDirectory(resource_path('my-assets'), 'my-assets')
@basset('my-assets/script.js')
```
--------------------------------
### Use Case: Performance Monitoring Listener
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/events.md
An example listener to track asset caching performance.
```php
class TrackAssetPerformance
{
public function handle(BassetCachedEvent $event):
{
metrics('assets.cached')->increment();
if (Str::isUrl($event->asset)) {
metrics('assets.cached.remote')->increment();
} else {
metrics('assets.cached.local')->increment();
}
}
}
```
--------------------------------
### assets() Method Usage
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/interfaces-and-contracts.md
Example implementation of the assets() method to map custom assets.
```php
public function assets(): void
{
Basset::map('jquery', 'https://custom-cdn.com/jquery.js');
Basset::map('bootstrap', 'https://custom-cdn.com/bootstrap.js');
}
```
--------------------------------
### Named Assets Mapping
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/README.md
Example of mapping a named asset to a specific source and attributes in the `AppServiceProvider`.
```php
// In AppServiceProvider boot()
Basset::map('jquery', 'https://code.jquery.com/jquery-3.6.0.min.js',
'defer' => true,
]);
// Later in Blade
@basset('jquery') {{-- Uses mapped source and attributes --}}
```
--------------------------------
### Custom Path Manager Implementation
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/interfaces-and-contracts.md
Example of implementing a custom asset path manager.
```php
namespace App\Services;
use Backpack\Basset\Contracts\AssetPathManagerInterface;
class CustomPathManager implements AssetPathManagerInterface
{
public function getBasePath(): string { }
public function isLocal(string $path): bool { }
public function getPathOnDisk(string $path): string { }
public function getCleanPath(string $path): string { }
}
```
```php
// Register in AppServiceProvider
$this->app->singleton(AssetPathManagerInterface::class, CustomPathManager::class);
```
--------------------------------
### Composer Scripts for Deployment
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/console-commands.md
Configuration for composer.json to automatically cache assets after installation or updates, suitable for VPS/Forge deployments.
```json
{
"scripts": {
"post-install-cmd": [
"php artisan storage:link",
"php artisan basset:cache"
],
"post-update-cmd": [
"php artisan storage:link",
"php artisan basset:cache"
]
}
}
# On deploy
composer install --no-dev
```
--------------------------------
### Fallback behavior of basset()
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/api-reference/helpers.md
Example demonstrating the fallback behavior of the basset() helper when an asset fails to internalize.
```blade
{{-- If basset('non-existent.js') fails, returns 'non-existent.js' --}}
{{-- Output: --}}
```
--------------------------------
### CustomAssetOverrides Implementation Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/interfaces-and-contracts.md
An example of a custom class implementing the OverridesAssets interface.
```php
// app/Providers/CustomAssetOverrides.php
namespace App\Providers;
use Backpack\Basset\OverridesAssets;
use Backpack\Basset\Facades\Basset;
class CustomAssetOverrides implements OverridesAssets
{
public function assets(): void
{
// Override a package asset
Basset::map('crud.select2', 'https://my-cdn.com/select2.min.js', [
'integrity' => 'sha384-specific-hash',
'crossorigin' => 'anonymous',
]);
// Add custom app assets
Basset::map('app.bundle', asset('js/app.bundle.js'));
Basset::map('app.styles', asset('css/app.css'));
// Override with local copy
Basset::map('analytics', base_path('vendor/analytics.js'));
}
}
```
--------------------------------
### basset:fresh Equivalent Command
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/console-commands.md
Shows that basset:fresh is equivalent to running basset:clear followed by basset:cache.
```bash
php artisan basset:clear && php artisan basset:cache
```
--------------------------------
### Service Container Binding Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/interfaces-and-contracts.md
Example of how the AssetPathManagerInterface is bound as a singleton in the service container.
```php
// In BassetServiceProvider::register()
$this->app->singleton(
Contracts\AssetPathManagerInterface::class,
AssetPathManager::class
);
```
--------------------------------
### Load-Once Behavior Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/README.md
Demonstrates how assets are automatically loaded only once per page, even if referenced multiple times across includes.
```blade
@basset('jquery')
@include('card') {{-- May also load @basset('jquery') --}}
@include('hero') {{-- May also load @basset('jquery') --}}
{{-- jQuery is loaded only once --}}
```
--------------------------------
### Configuration Publishing
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/service-provider.md
Users can publish the configuration file using the 'vendor:publish' Artisan command to customize settings.
```bash
php artisan vendor:publish --provider="Backpack\Basset\BassetServiceProvider"
```
--------------------------------
### Code Blocks Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/api-reference/asset-hash-manager.md
Example of how @bassetBlock directives use content hashes for generated filenames.
```blade
@bassetBlock('init')
@endBassetBlock
```
```text
This creates a file like basset/init-xyz12345.js with the content. When the code changes, a new hash is generated and a new file is created, automatically cache-busting.
```
--------------------------------
### Development Mode Detection Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/api-reference/asset-hash-manager.md
Example of how AssetHashManager is used in development mode to detect content changes.
```php
// In BassetManager::loadAsset()
if ($this->dev) {
if ($mapped->getContentHash() !== $asset->generateContentHash()) {
// Content changed, re-download/re-cache
return $this->replaceAsset($asset, $mapped, $output);
}
}
```
--------------------------------
### Helper Function Equivalence
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/api-reference/basset-facade.md
Demonstrates the equivalence between the helper function and direct facade/method calls.
```php
Basset::basset($asset, false);
return Basset::getUrl($asset);
```
--------------------------------
### Constructor
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/api-reference/cache-map.md
Initializes the cache map and loads existing data from disk if BASSET_CACHE_MAP=true.
```php
public function __construct(
FilesystemAdapter $disk,
string $basePath
)
```
--------------------------------
### Custom Path or Hash Manager Implementation
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/README.md
Shows how to implement custom path or hash managers and register them in the service container.
```php
namespace App\Services;
use Backpack\Basset\Contracts\AssetPathManagerInterface;
class CustomPathManager implements AssetPathManagerInterface
{
// implement methods
}
// In AppServiceProvider
$this->app->singleton(
AssetPathManagerInterface::class,
CustomPathManager::class
);
```
--------------------------------
### Accessing Configuration Values
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/configuration.md
Demonstrates how to access Basset configuration values in PHP, including using environment variables.
```php
config('backpack.basset.key_name');
// Or with env() for values that support environment variables
env('BASSET_DEV_MODE', false);
```
--------------------------------
### @bassetBlock() Examples
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/blade-directives.md
Examples showcasing the @bassetBlock() directive for internalizing blocks of inline JavaScript or CSS, saving them to files for better performance and preventing duplicate loading.
```blade
{{-- Inline initialization script --}}
@bassetBlock('page-init')
@endBassetBlock
{{-- Inline styles --}}
@bassetBlock('custom-theme')
@endBassetBlock
{{-- In reusable component --}}
@bassetBlock('card-script')
@endBassetBlock
```
--------------------------------
### Publish Configuration File
Source: https://github.com/laravel-backpack/basset/blob/main/readme.md
Command to publish the Basset configuration file for customization.
```bash
# optional
php artisan vendor:publish --provider="Backpack\Basset\BassetServiceProvider"
```
--------------------------------
### provides() Method
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/service-provider.md
Declares the services this provider provides to optimize the container.
```php
public function provides(): array
{
return ['basset'];
}
```
--------------------------------
### Dependency Injection for AssetHashManager
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/interfaces-and-contracts.md
Example of injecting AssetHashManagerInterface into a method.
```php
public function myFunction(AssetHashManagerInterface $hasher) {
$hash = $hasher->generateHash($content);
}
```
--------------------------------
### Load Vendor Package Assets
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/README.md
Demonstrates loading assets from a vendor package, either directly or by copying an entire directory and then referencing it.
```blade
@basset(base_path('vendor/org/package/dist/plugin.js'))
// Or copy entire directory:
// @bassetDirectory(base_path('vendor/org/package/assets'), 'org-pkg')
// @basset('org-pkg/plugin.js')
```
--------------------------------
### Usage of StatusEnum
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/types.md
Example of how to use the StatusEnum in PHP.
```php
$status = Basset::basset('jquery');
if ($status === StatusEnum::INTERNALIZED) {
// Asset was successfully downloaded
}
if (in_array($status, [StatusEnum::DISABLED, StatusEnum::INVALID])) {
// Asset could not be internalized; fallback behavior
}
// Access the case value
echo $status->value; // 'Already loaded'
```
--------------------------------
### fetchContent() Method
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/api-reference/basset-manager.md
Downloads content from a URL using Guzzle HTTP client. Respects BASSET_VERIFY_SSL_CERTIFICATE config.
```php
public function fetchContent(string $url): string
```
--------------------------------
### getMap() Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/api-reference/cache-map.md
Returns the entire in-memory cache map as an associative array.
```php
$allAssets = $cacheMap->getMap();
// ['jquery' => [...], 'bootstrap' => [...], ...]
foreach ($allAssets as $name => $assetData) {
echo "$name: {$assetData['asset_path']}";
}
```
--------------------------------
### getPathOnDisk() Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/api-reference/asset-path-manager.md
Converts an asset filename or URL into a disk-relative path.
```php
$manager->getPathOnDisk('https://example.com/jquery.js');
// Returns: 'basset/jquery.js'
$manager->getPathOnDisk(storage_path('uploads/custom.js'));
// Returns: 'basset/custom.js'
```
--------------------------------
### FilesystemAdapter Usage
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/types.md
Example demonstrating the use of Laravel's Storage facade with a filesystem disk, as utilized by Basset for disk operations.
```php
$disk = Storage::disk('basset');
$disk->put('file.js', $content, 'public');
$exists = $disk->exists('file.js');
$disk->delete('file.js');
```
--------------------------------
### getBasePath() Example
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/api-reference/asset-path-manager.md
Retrieves the configured base path for asset storage.
```php
$manager->getBasePath(); // 'basset/'
```
--------------------------------
### BassetDirectory Directive
Source: https://github.com/laravel-backpack/basset/blob/main/readme.md
Example of using the @bassetDirectory directive to internalize an entire non-public directory.
```blade
+ @bassetDirectory(resource_path('package-1.0.0/'), 'package-1.0.0')
+ @basset('package-1.0.0/plugin.min.js')
```
--------------------------------
### basset:cache Syntax
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/console-commands.md
Scans Blade templates for Basset directives and pre-internalizes all referenced assets.
```bash
php artisan basset:cache
```
--------------------------------
### BassetArchive Directive
Source: https://github.com/laravel-backpack/basset/blob/main/readme.md
Example of using the @bassetArchive directive to internalize assets from a ZIP archive.
```blade
+ @bassetArchive('https://github.com/author/package-dist/archive/refs/tags/1.0.0.zip', 'package-1.0.0')
+ @basset('package-1.0.0/plugin.min.js')
```
--------------------------------
### Defer All Scripts Globally
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/README.md
Configuration option to globally defer all script assets.
```php
// config/backpack/basset.php
'script_attributes' => [
'defer' => true,
],
```
--------------------------------
### Custom Hash Manager Implementation
Source: https://github.com/laravel-backpack/basset/blob/main/_autodocs/interfaces-and-contracts.md
Example of how to implement a custom AssetHashManagerInterface in PHP.
```php
namespace App\Services;
use Backpack\Basset\Contracts\AssetHashManagerInterface;
class CustomHashManager implements AssetHashManagerInterface
{
public function generateHash(string $content): string { }
public function appendHashToPath(string $path, string $hash): string { }
public function validateHash(string $content, string $hash): bool { }
}
// Register in AppServiceProvider
$this->app->singleton(AssetHashManagerInterface::class, CustomHashManager::class);
```
--------------------------------
### Basset vs Asset Helper
Source: https://github.com/laravel-backpack/basset/blob/main/readme.md
Demonstrates how to switch from Laravel's default asset() helper to Basset's basset() helper for including assets, including those from non-public locations or external URLs.
```blade
{{-- if you're used to Laravel's asset helper: --}}
{{-- just change asset() to basset() and you can point to non-public files too, for example: --}}