Use App::optimizes() in Service Providers to register custom cache generation commands that automatically run during php artisan optimize.
When building packages or domain systems that generate custom caches (such as compiling dynamic permission matrices, building search indexes, or caching OpenAPI schemas), developers often have to remember separate custom CLI commands during deployment.
Laravel allows service providers to register tasks into php artisan optimize and php artisan optimize:clear.
Registering Optimization Hooks in AppServiceProvider
namespace App\Providers;
use Illuminate\Support\Facades\App;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function boot(): void
{
// Automatically runs during 'php artisan optimize' and clears during 'optimize:clear'
App::optimizes(
optimize: 'permissions:cache',
clear: 'permissions:clear',
key: 'permissions'
);
}
}
Deploying
Now running php artisan optimize in your CI/CD pipeline automatically compiles your custom caches alongside route, config, and view caches.
Summary
- Integrates custom caching commands into standard Laravel deployment lifecycles.
- Automatically handles cache clearing with
php artisan optimize:clear. - Keeps deployment scripts clean and unified.
Related Tips
View all tips →Use Laravel's remember_token as a Revocation Signal
Use Laravel's remember_token as an invalidation signal for saved account-switching state without maintaining a dedicated token or revocation table.
Extract Dominant Colors and Handle HEIC/AVIF Images in Laravel 13.24
Laravel 13.24 adds dominantColor() to the Image API for extracting the primary color of an image, plus native support for HEIC and AVIF formats.