The Laravel Framework Console Kernel

December 1, 2016 —John Koster

The console kernel exposes many different public methods. This article will not cover all of the public methods available, but only the most useful ones.

#all

The all method is used to get all of the commands that have been registered with the console application. The commands will be returned as an array with the command name as the key and the command's class instance as the value. This method will bootstrap the application as well as force the loading of deferred service providers.

The following example demonstrates one way to call this method:

1<?php
2 
3use Illuminate\Contracts\Console\Kernel;
4use Illuminate\Support\Facades\Artisan;
5 
6// Get a console instance.
7$console = app(Kernel::class);
8 
9// Get the registered console commands.
10$registeredCommands = $console->all();
11 
12 
13// The following facade method would be equivalent
14$registeredCommands = Artisan::all();

After the above code has executed, the $registeredCommands variable would be an array containing all of the console commands that have been registered.

#registerCommand($command)

The registerCommand method is used to register a command with the console application. In older versions of Laravel, it was required to use this method to register any custom commands with the console kernel. The registerCommand method expects a $command argument to be supplied; the supplied $command must ultimately be an instance of Symfony\Component\Console\Command\Command (this means that any Symfony command or Laravel derived command would be acceptable).

The following example demonstrates the registerCommand method usage:

1<?php
2 
3use Illuminate\Support\Facades\Artisan;
4use Illuminate\Contracts\Console\Kernel;
5use App\Console\Commands\Inspire;
6 
7// Get a console instance.
8$console = app(Kernel::class);
9 
10// Register the default `Inspire`
11// command with the application:
12$console->registerCommand(app(Inspire::class));
13 
14// The following facade method would be equivalent:
15Artisan::registerCommand(app(Inspire::class)):

After the above code has executed, the Inspire command would be available to the console application. We could check to ensure its existence by using the all method:

1<?php
2 
3// Determine if the `Inspire` command was registered or not.
4$inspireRegistered = array_key_exists(
5 'inspire',
6 $console->all()
7);

The above coder would check to see if a particular console command has been registered with the application. It does this by checking to see if the name of the command is included in the array returned by the kernel's all method. Since the all method returns an array where the registered command names are the keys, this would be sufficient for an existence test.

#call($command, array $parameters = [])

The call method is used to execute an Artisan command from somewhere else in your applications code. It accepts the name of the command via an argument supplied for the $command parameter and an array of $parameters that should be supplied to the command. The exit code returned by the command will be the return value of the call method.

The following simple example calls the inspire (assuming it has been registered) command from some application code:

1<?php
2 
3use Illuminate\Contracts\Console\Kernel;
4use Illuminate\Support\Facades\Artisan;
5 
6// Get a console instance.
7$console = app(Kernel::class);
8 
9// Execute the inspire command:
10$console->call('inspire');
11 
12// The following facade method would be equivalent:
13Artisan::call('inspire');

The inspire command outputs a random inspiration quote. The outputted quote would not be returned from the call method. The output method would be used to get the output from the last ran Artisan command:

1<?php
2 
3// Get the output from the last command.
4$output = $console->output();
5 
6// The following facade method would be equivalent:
7$output = Artisan::output();

Assuming the last ran command was the inspire command, the $output variable would contain one of the randomly chosen inspiration quotes.

The following example demonstrate how to execute Artisan commands while also supplying arguments and options. Arguments are supplied as an array to the $parameters parameter. The supplied array should contain the name of the argument or option as the key and the associated value as the value for the given key.

We can take the following Artisan command that would normally be executed at the command line:

1php artisan make:migration create_drinks_table --path=database/setup_migrations

And call it directly form our application like so:

1<?php
2 
3use Illuminate\Contracts\Console\Kernel;
4use Illuminate\Support\Facades\Artisan;
5 
6// Get a console instance.
7$console = app(Kernel::class);
8 
9// Generate a new migration from within our
10// application using the `make:migration`
11// Artisan command.
12$console->call('make:migration', [
13 'name' => 'create_drinks_table',
14 '--path' => 'database/setup_migrations'
15]);

It is important to note that the name of the argument or option must be supplied as the key if they are used, even if they are not required when executing the command from the terminal. The names of options must also start with the -- prefix.

#output

The output method is used to retrieve the generated output from the Artisan console command that was executed last using the call method. The following example will assume that the inspire command has been registered.

First we need to call the inspire command:

1<?php
2 
3use Illuminate\Contracts\Console\Kernel;
4use Illuminate\Support\Facades\Artisan;
5 
6// Get a console instance.
7$console = app(Kernel::class);
8 
9// Execute the inspire command:
10$console->call('inspire');
11 
12// The following facade method would be equivalent:
13Artisan::call('inspire');

To retrieve the output from the inspire command, we can use the output method:

1<?php
2 
3// Get the output from the last command.
4$output = $console->output();
5 
6// The following facade method would be equivalent:
7$output = Artisan::output();

After the above code has executed, the $output variable would contain the output from the inspire command, which should be a randomly selected inspirational message.

The output method does not remove any newline characters or special characters from the returned output. It is important to keep this mind when presenting command output to users.

#queue($command, array $parameters = [])

The queue method is called in exactly the same way as the call method. It accepts the name of the command via an argument supplied for the $command parameter and an array of $parameters that should be supplied to the command. The exit code returned by the command will be the return value of the call method. Just like with the call method, the name of arguments and options must be supplied as the key if they are used, even if they are not required when executing the command from the terminal. The names of options must also start with the -- prefix.

The major difference between the queue and call methods is that the queue method will cause the Artisan command to be processed in the background by the configured queue workers. The following example demonstrates how to call the queue method (the same example will be used from the call method section; generally more intensive tasks would be queued instead of generating migrations, such as the sending of emails):

1<?php
2 
3use Illuminate\Contracts\Console\Kernel;
4use Illuminate\Support\Facades\Artisan;
5 
6// Get a console instance.
7$console = app(Kernel::class);
8 
9// Generate a new migration from within our
10// application using the `make:migration`
11// Artisan command.
12$console->queue('make:migration', [
13 'name' => 'create_drinks_table',
14 '--path' => 'database/setup_migrations'
15]);
16 
17// The following facade method would be equivalent:
18Artisan::queue('make:migration', [
19 'name' => 'create_drinks_table',
20 '--path' => 'database/setup_migrations'
21]);

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.