Artisan Console Flashcards

1
Q

What is the the command for creating a new command?

A

php artisan make:command {command name}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

After generating a new command what properties will need to be filled in?

A

You should fill in the signature and description properties of the class, which will be used when displaying your command on the list screen.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

In which method is the the command logic stored?

A

The handle method will be called when your command is executed. You may place your command logic in this method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are Closure Commands?

A

Closure based commands provide an alternative to defining console commands as classes. In the same way that route Closures are an alternative to controllers, think of command Closures as an alternative to command classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

In which file are Closure Commands defined?

A

Within the routes/console.php file, you may define all of your Closure based routes using the Artisan::command method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the two arguments accepted by Artisan::command method?

A

The command method accepts two arguments: the command signature and a Closure which receives the commands arguments and options:

Artisan::command(‘build {project}’, function ($project) {
$this->info(“Building {$project}!”);
});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

In addition to receiving command arguments and options what else can be received by a command class and closure command?

A

Command classes and command Closures may also type-hint additional dependencies that you would like resolved out of the service container.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the purpose of the describe method?

A

When defining a Closure based command, you may use the describe method to add a description to the command. This description will be displayed when you run the php artisan list or php artisan help commands:

Artisan::command(‘build {project}’, function ($project) {
$this->info(“Building {$project}!”);
})->describe(‘Build the project’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How is user input gathered for a command?

A

When writing console commands, it is common to gather input from the user through arguments or options. Laravel makes it very convenient to define the input you expect from the user using the signature property on your commands. The signature property allows you to define the name, arguments, and options for the command in a single, expressive, route-like syntax.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How is a command defined when receiving arguments?

A

All user supplied arguments and options are wrapped in curly braces. In the following example, the command defines one required argument: user:

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = ‘email:send {user}’;

You may also make arguments optional and define default values for arguments:

// Optional argument...
email:send {user?}
// Optional argument with default value...
email:send {user=foo}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How is a command defined when receiving options?

A

Options, like arguments, are another form of user input. Options are prefixed by two hyphens (–) when they are specified on the command line. There are two types of options: those that receive a value and those that don’t. Options that don’t receive a value serve as a boolean “switch”. Let’s take a look at an example of this type of option:

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = ‘email:send {user} {–queue}’;

In this example, the –queue switch may be specified when calling the Artisan command. If the –queue switch is passed, the value of the option will be true. Otherwise, the value will be false:

php artisan email:send 1 –queue

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How is a command defined when receiving an option with a value?

A

If the user must specify a value for an option, suffix the option name with a = sign:

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = ‘email:send {user} {–queue=}’;

In this example, the user may pass a value for the option like so:

php artisan email:send 1 –queue=default

You may assign default values to options by specifying the default value after the option name. If no option value is passed by the user, the default value will be used:

email:send {user} {–queue=default}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How is a command defined when receiving an option with a shortcut?

A

To assign a shortcut when defining an option, you may specify it before the option name and use a | delimiter to separate the shortcut from the full option name:

email:send {user} {–Q|queue}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How is a command defined when expecting an array as input?

A

If you would like to define arguments or options to expect array inputs, you may use the * character. First, let’s take a look at an example that specifies an array argument:

email:send {user*}

When calling this method, the user arguments may be passed in order to the command line. For example, the following command will set the value of user to [‘foo’, ‘bar’]:

php artisan email:send foo bar

When defining an option that expects an array input, each option value passed to the command should be prefixed with the option name:

email:send {user} {–id=*}

php artisan email:send –id=1 –id=2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How is a command defined when an argument or option has a description?

A

You may assign descriptions to input arguments and options by separating the parameter from the description using a colon. If you need a little extra room to define your command, feel free to spread the definition across multiple lines:

/**
 * The name and signature of the console command.
 *
 * @var string
 */
protected $signature = 'email:send
                        {user : The ID of the user}
                        {--queue= : Whether the job should be queued}';
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the methods that allow access to a commands input?

A

argument() and option()

17
Q

How would you retrieve all arguments or options in the form of an array from an executing command?

A

If you need to retrieve all of the arguments as an array, call the arguments method:

$arguments = $this->arguments();

Options may be retrieved just as easily as arguments using the option method:

// Retrieve all options...
$options = $this->options();
18
Q

How would you retrieve a specific argument or option from an executing command?

A

$userId = $this->argument(‘user’);

$queueName = $this->option(‘queue’);

19
Q

What is returned if an argument or option does not exist?

A

If the argument or option does not exist, null will be returned.

20
Q

What is the purpose of the ask() method?

A

The ask method will prompt the user with the given question, accept their input, and then return the user’s input back to your command:

/**
 * Execute the console command.
 *
 * @return mixed
 */
public function handle()
{
    $name = $this->ask('What is your name?');
}
21
Q

What is the purpose of the secret() method?

A

The secret method is similar to ask, but the user’s input will not be visible to them as they type in the console. This method is useful when asking for sensitive information such as a password:

$password = $this->secret(‘What is the password?’);

22
Q

What is the purpose of the confirm() method?

A

If you need to ask the user for a simple confirmation, you may use the confirm method. By default, this method will return false. However, if the user enters y or yes in response to the prompt, the method will return true.

if ($this->confirm('Do you wish to continue?')) {
    //
}
23
Q

What is the purpose of the anticipate() method?

A

The anticipate method can be used to provide auto-completion for possible choices. The user can still choose any answer, regardless of the auto-completion hints:

$name = $this->anticipate(‘What is your name?’, [‘Taylor’, ‘Dayle’]);

24
Q

What is the purpose of the choice() method?

A

If you need to give the user a predefined set of choices, you may use the choice method. You may set the default value to be returned if no option is chosen:

$name = $this->choice(‘What is your name?’, [‘Taylor’, ‘Dayle’], $default);

25
Q

What methods can be used to sent output to the console?

A

To send output to the console, use the line, info, comment, question and error methods. Each of these methods will use appropriate ANSI colors for their purpose. For example, let’s display some general information to the user. Typically, the info method will display in the console as green text:

/**
 * Execute the console command.
 *
 * @return mixed
 */
public function handle()
{
    $this->info('Display this on the screen');
}
26
Q

Which method will display an error message to the console?

A

To display an error message, use the error method. Error message text is typically displayed in red:

$this->error(‘Something went wrong!’);

27
Q

Which method will display an uncolored message to the console?

A

If you would like to display plain, uncolored console output, use the line method:

$this->line(‘Display this on the screen’);

28
Q

What is the purpose of the table() method?

A

The table method makes it easy to correctly format multiple rows / columns of data. Just pass in the headers and rows to the method. The width and height will be dynamically calculated based on the given data:

$headers = [‘Name’, ‘Email’];

$users = App\User::all([‘name’, ‘email’])->toArray();

$this->table($headers, $users);

29
Q

How is a progress bar displayed in the console?

A

For long running tasks, it could be helpful to show a progress indicator. Using the output object, we can start, advance and stop the Progress Bar. First, define the total number of steps the process will iterate through. Then, advance the Progress Bar after processing each item:

$users = App\User::all();

$bar = $this->output->createProgressBar(count($users));

foreach ($users as $user) {
$this->performTask($user);

$bar->advance(); }

$bar->finish();
For more advanced options, check out the Symfony Progress Bar component documentation.

30
Q

Once a new command has been created, what is required in order for the command to work?

A

Once your command is finished, you need to register it with Artisan. All commands are registered in the app/Console/Kernel.php file. Within this file, you will find a list of commands in the commands property. To register your command, simply add the command’s class name to the list. When Artisan boots, all the commands listed in this property will be resolved by the service container and registered with Artisan:

protected $commands = [
Commands\SendEmails::class
];

31
Q

What is the purpose of the Artisan call() method?

A

Sometimes you may wish to execute an Artisan command outside of the CLI. For example, you may wish to fire an Artisan command from a route or controller. You may use the call method on the Artisan facade to accomplish this. The call method accepts the name of the command as the first argument, and an array of command parameters as the second argument. The exit code will be returned:

Route::get(‘/foo’, function () {
$exitCode = Artisan::call(‘email:send’, [
‘user’ => 1, ‘–queue’ => ‘default’
]);

    //
});
32
Q

What is the purpose of the Artisan queue() method?

A

Using the queue method on the Artisan facade, you may even queue Artisan commands so they are processed in the background by your queue workers. Before using this method, make sure you have configured your queue and are running a queue listener:

Route::get(‘/foo’, function () {
Artisan::queue(‘email:send’, [
‘user’ => 1, ‘–queue’ => ‘default’
]);

    //
});
33
Q

How do you specify the value of an option that does not accept string values?

A

If you need to specify the value of an option that does not accept string values, such as the –force flag on the migrate:refresh command, you may pass true or false:

$exitCode = Artisan::call(‘migrate:refresh’, [
‘–force’ => true,
]);

34
Q

What method is used to call another command from other commands and how?

A

Sometimes you may wish to call other commands from an existing Artisan command. You may do so using the call method. This call method accepts the command name and an array of command parameters:

/**
 * Execute the console command.
 *
 * @return mixed
 */
public function handle()
{
    $this->call('email:send', [
        'user' => 1, '--queue' => 'default'
    ]);
    //
}
35
Q

What is the purpose of the callSilent() method?

A

If you would like to call another console command and suppress all of its output, you may use the callSilent method. The callSilent method has the same signature as the call method:

$this->callSilent(‘email:send’, [
‘user’ => 1, ‘–queue’ => ‘default’
]);