Search

The Laravel Application Console Kernel

December 1, 2016 —John Koster

Each application contains a console kernel. The console kernel is defined in the app/Console/Kernel.php (this will be referred to as the application console kernel) file. The kernel class that exists in that file extends Laravel's console kernel (which can be found in the vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php file; this kernel will be defined as the framework console kernel). From the applications point of view, the application console kernel is responsible for specifying which custom commands should be made available to users and when to automatically execute various commands and tasks (by using the task scheduler).

It is important to note that while this the application and console kernels are separated into their own sections, they are the same thing, and are only being differentiated on this site to make explaining them easier. The "application" console kernel, located in the app/Console/Kernel.php file, is simply a convenient way to expose the features of the "framework" console kernel to your application. Any features, limitations, or otherwise that are discussed in the section "The Framework Console Kernel" can be applied to the application console kernel class.

The application console kernel is mainly used for two purposes in relation to your application:

  • Registering commands for use;
  • Scheduling when various commands and tasks should be ran (by using the scheduler).

The kernel is fairly simple by default (the vast majority of the logic is contained within the framework's console kernel):

1<?php
2 
3namespace App\Console;
4 
5use Illuminate\Console\Scheduling\Schedule;
6use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
7 
8class Kernel extends ConsoleKernel
9{
10 /**
11 * The Artisan commands provided by your application.
12 *
13 * @var array
14 */
15 protected $commands = [
16 ];
17 
18 /**
19 * Define the application's command schedule.
20 *
21 * @param \Illuminate\Console\Scheduling\Schedule $schedule
22 * @return void
23 */
24 protected function schedule(Schedule $schedule)
25 {
26 
27 }
28 
29 /**
30 * Register the Closure based commands for the application.
31 *
32 * @return void
33 */
34 protected function commands()
35 {
36 
37 }
38 
39}

The kernel has an empty $commands array and an empty schedule() method. The $commands property is used to specify which commands are to be loaded when the console application (Artisan) starts. In the introduction to this chapter the following error message was discussed briefly:

1[Symfony\Component\Console\Exception\CommandNotFoundException]
2 Command "inspire" is not defined.

The most common cause of this error is the command that a user is attempting to run has not been added to the $commands array. The act of adding the name of a class to the $commands array is referred to as registering the command with the application.

To register a command with the application, simply add the fully qualified name to the command class to the $commands array. In earlier versions of Laravel (targeting older versions of PHP), this was done by specifying the class name as a string; however, starting with PHP version 5.5, the ::class class constant. It is also important to remember to include the class if it is located in a different namespace (using the use PHP keyword). Application commands are, by default, stored within the app/Console/Commands/ directory (under the App\Console\Commands namespace). Alternatively, commands can be registered using the command shorthand syntax. The commands method is automatically called by the framework and is a convenient place to register commands using the shorthand syntax.

In the introductory article to this series Writing Custom Laravel Artisan Commands: An Introduction, the inspire Artisan command was used as an example. This command is stored within the app/Console/Commands/Inspire.php file and can be accessed by using the App\Console\Commands\Inspire class (this is the fully qualified name of the Inspire command). The following examples demonstrate how to register the Inspire command with the application.

The following example uses the ::class class constant, and is generally the recommended way of referring to classes:

1<?php
2 
3namespace App\Console;
4 
5use Illuminate\Console\Scheduling\Schedule;
6use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
7 
8class Kernel extends ConsoleKernel
9{
10 /**
11 * The Artisan commands provided by your application.
12 *
13 * @var array
14 */
15 protected $commands = [
16 // Remember that namespaces are relative so the
17 // `Commands\Inspire` class is resolved in
18 // relation to the current App\Console
19 // namespace to get the final class.
20 Commands\Inspire::class
21 ];
22 
23 
24 // Schedule method omitted.
25 
26}

The following example registers the Inspire command by supplying the fully qualified class name as a string:

1<?php
2 
3namespace App\Console;
4 
5use Illuminate\Console\Scheduling\Schedule;
6use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
7 
8class Kernel extends ConsoleKernel
9{
10 /**
11 * The Artisan commands provided by your application.
12 *
13 * @var array
14 */
15 protected $commands = [
16 'App\Console\Commands\Inspire'
17 ];
18 
19 
20 // Schedule method omitted.
21 
22}

Scheduling commands and tasks will be discussed in its own article.

The discussion of the Laravel Console kernel continues in the article The Laravel Framework Console Kernel article.

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.