The Laravel Optimize command is a command that is used to optimize the Laravel application. It is used to optimize the
application by compiling the application's configuration files, routes, and views into a single file.
This command is used to improve the performance of the Laravel application by reducing the number of files that need to
be loaded when the application is running.
It is good practice to use this command when deploying the Laravel application to a production environment.
php artisan optimize
Under the hood, the optimize
command is calling the optimize
method of the OptimizeCommand
class. This class is
located in the Illuminate\Foundation\Console
namespace.
It will run the following commands:
$this->call('config:cache');
$this->call('event:cache');
$this->call('route:cache');
$this->call('view:cache');
But there is a way you can extend this command by adding your own custom commands into the optimize
command.
For example lets say you want tp run a command on deployment that will generate a new sitemap
php artisan sitemap:generate
and you want to run this command when you run php artisan optimize
command.
To do this you can add a new command into the ServiceProviders to add into the optimize
command. This is done by
using the new optimizes
method that is available in version Laravel v11.27.1.
public function boot(): void
{
$this->optimizes(
optimize: SitemapGenerateCommand::class,
);
}
Now this will run the SitemapGenerateCommand
command when you run php artisan optimize
command.
Top comments (0)