Customizing Laravel Optimization with --except
Need to customize your Laravel optimization process? The new --except option lets you selectively skip commands during optimization, giving you precise control over the optimization process.
When optimizing Laravel applications, you might want to skip certain optimization steps based on your development workflow or specific requirements. The new --except option provides a flexible way to exclude commands from the optimization process, whether you're targeting specific commands or entire command groups.
Let's see how it works:
// Skip a specific command
php artisan optimize --except route:cache
// Skip by command key
php artisan optimize --except route
// Skip multiple commands
php artisan optimize:clear --except route,view:cache
Real-World Example
Here's how you might use it in different deployment scenarios:
# Development deployment script
function deploy_dev() {
# Skip route caching in development
php artisan optimize --except route:cache
}
# Staging deployment script
function deploy_staging() {
# Skip config and route caching for testing
php artisan optimize --except config,route
}
# Custom optimization script
function optimize_custom() {
if [[ $CI_ENVIRONMENT == "testing" ]]; then
# Skip view and route caching in test environment
php artisan optimize --except view:cache,route:cache
else
# Run all optimizations
php artisan optimize
fi
}
The --except option brings new flexibility to Laravel's optimization process. Instead of managing multiple commands or creating custom scripts, you can now easily exclude specific optimization steps with a single command. This is particularly valuable when working across different environments or when certain optimizations might interfere with your development workflow.
Think of it as a filter for your optimization process - you get to choose which optimizations to skip while running all other optimizations as normal.
For example, when debugging route issues, you can skip route caching while still benefiting from other optimizations, making your debugging process more efficient.
If this guide was helpful to you, subscribe to my daily newsletter and give me a follow on X/Twitter and Bluesky. It helps a lot!