Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

trevorpan's avatar

clarification with how to set console/kernel.php

Hello there ~

https://laravel.com/docs/8.x/scheduling#defining-schedules

On the above link the protected $commands = [.. is empty and so I'm finding it confusing. The protected function commands() method tells this Kernel file where to find custom commands.

Am I declaring it improperly in protected $commands?

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        Commands\DeadlineHasPassed::class,
        Commands\GenerateSitemap::class,
    ];

.... // $scheduled tasks

/**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
0 likes
6 replies
fylzero's avatar

@trevorpan

If you are trying to define commands add to the $commands section as you have done.

https://laravel.com/docs/8.x/artisan#registering-commands

If you are trying to define the schedule you'll need to add the schedule() method as the docs suggest.

https://laravel.com/docs/8.x/scheduling#defining-schedules

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        Commands\DeadlineHasPassed::class,
        Commands\GenerateSitemap::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // USE WHATEVER YOUR COMMANDS ACTUALLY ARE
	$schedule->command('deadline:passed')->daily();
	$schedule->command('sitemap:generate')->daily();
    }
}
1 like
trevorpan's avatar

@fylzero You know what's so strange, is that I just tried commenting out:

 protected $commands = [
 //       Commands\DeadlineHasPassed::class,
 //       Commands\GenerateSitemap::class,
    ];

Ran CI and then the bloody Deadline email sent! Could having the directory in the commands()create a sort of strange loop?

Or... is it that because $this->load(__DIR__.'/Commands'); then creates /Commands/Commands/DeadlineHasPassed::class,? Was that the issue maybe?

fylzero's avatar

From the docs:

If necessary, you may manually register commands by adding the command's class name to the $commands property of your App\Console\Kernel class. When Artisan boots, all the commands listed in this property will be resolved by the service container and registered with Artisan

You don't need to add commands to $commands because they are already loaded by default, that is correct.

2 likes
trevorpan's avatar

geez, I knew something was wrong.

I might try to contribute and add a note about this on the docs.

Appreciate your help!

trevorpan's avatar

@fylzero Had one more thought.

So, for an example, this property declaration protected $commands = [could be used if you have commands in a large app that are stored outside the traditional App\Console directory?

fylzero's avatar

@trevorpan Just like the example for the commands() method shows the ability to load outside the directory, I think this just offers flexibility to load commands directly/specifically whether they are in the App\Console directory or not. It seems to be an either or thing though, since clearly defining specifically and with the directory at the same time seems to break.

The bottom line for me is if you add commands inside the App\Console directory, you don't need to do any special wiring for that to work. That is probably the most common practice/path of least resistance. So I would just use that 100% of the time tbh.

2 likes

Please or to participate in this conversation.