Artisan Console Flashcards
What is the the command for creating a new command?
php artisan make:command {command name}
After generating a new command what properties will need to be filled in?
You should fill in the signature and description properties of the class, which will be used when displaying your command on the list screen.
In which method is the the command logic stored?
The handle method will be called when your command is executed. You may place your command logic in this method.
What are Closure Commands?
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.
In which file are Closure Commands defined?
Within the routes/console.php file, you may define all of your Closure based routes using the Artisan::command method.
What are the two arguments accepted by Artisan::command method?
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}!”);
});
In addition to receiving command arguments and options what else can be received by a command class and closure command?
Command classes and command Closures may also type-hint additional dependencies that you would like resolved out of the service container.
What is the purpose of the describe method?
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 is user input gathered for a command?
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 is a command defined when receiving arguments?
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 is a command defined when receiving options?
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 is a command defined when receiving an option with a value?
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 is a command defined when receiving an option with a shortcut?
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 is a command defined when expecting an array as input?
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 is a command defined when an argument or option has a description?
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}';