DEV Community

Paulund
Paulund

Posted on • Originally published at paulund.co.uk

Extend Laravel Optimize Command

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
Enter fullscreen mode Exit fullscreen mode

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');
Enter fullscreen mode Exit fullscreen mode

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,
    );
}
Enter fullscreen mode Exit fullscreen mode

Now this will run the SitemapGenerateCommand command when you run php artisan optimize command.

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →