title | author | format | ||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Laravel Commands |
Vladimir Lelicanin - SAE Institute |
|
- Laravel provides a powerful Artisan command-line interface
- It allows for easy and automated management of various tasks
- However, sometimes we need to create our own custom commands
- Creating a custom command is straightforward
- Just create a new class that extends Laravel's
Illuminate\Console\Command
class - Implement the
handle()
method that contains the command logic
namespace App\Console\Commands;
use Illuminate\Console\Command;
class GreetUser extends Command
{
protected $signature = 'greet {name}';
protected $description = 'Greet the user by name.';
public function handle()
{
$name = $this->argument('name');
$this->info("Hello, $name! How are you today?");
}
}
- To register the command, add it to the
commands
array inapp/Console/Kernel.php
- Alternatively, you can use the
load()
method in the same file to auto-discover commands in a directory
protected $commands = [
\App\Console\Commands\GreetUser::class,
];
- The
$signature
property specifies the command name and any parameters or options - Commands are called using
php artisan
followed by the command name and any arguments
protected $signature = 'greet {name} {--loud}';
php artisan greet John # outputs "Hello, John!"
php artisan greet John --loud # outputs "HELLO, JOHN!"
- The
$description
property provides a brief explanation of what the command does - It is displayed when using the
php artisan list
command to show all available commands
protected $description = 'Greet the user by name.';
- Laravel provides several helper methods to output text to the console
info()
: standard output in green textcomment()
: comment output in yellow texterror()
: error output in red textline()
: plain text output
$this->info('This is some information');
$this->comment('This is a comment');
$this->error('This is an error');
$this->line('Just plain text');
- Laravel can gather input from the user in various ways
argument()
: retrieve an argument passed to the commandoption()
: retrieve an option passed to the commandask()
: prompt the user to enter a valueconfirm()
: prompt the user to confirm a choice
$name = $this->argument('name');
if ($this->option('loud')) {
$this->info(strtoupper("Hello, $name!"));
} else {
$this->info("Hello, $name!");
}
- Laravel can output data in a nicely formatted table
- Use the
table()
method and pass in an array of data
$data = [
['Name', 'Age'],
['John', 20],
['Jane', 25],
];
$this->table(['Name', 'Age'], $data);
- Laravel can display a progress bar for long-running tasks
- Use the
output
object to create aProgressBar
instance
$bar = $this->output->createProgressBar(10);
$bar->start();
foreach (range(1, 10) as $i) {
sleep(1);
$bar->advance();
}
$bar->finish();
- Laravel commands can have additional options for customization
$name
: Specify the command name$description
: Set the command description$hidden
: Hide the command from the list of available commands$help
: Provide help text for the command
- Laravel provides a few events that are fired during command execution
Illuminate\Console\Events\CommandStarting
: Fired when the command is startingIlluminate\Console\Events\CommandFinished
: Fired when the command has finishedIlluminate\Console\Events\CommandErrored
: Fired when the command throws an exception
- Custom Laravel commands are easy to create and provide a powerful way to automate tasks
- They can be registered and used just like built-in Artisan commands
- Laravel provides many helpful helpers for handling input and output
- Laravel Documentation: Command Events
- Laravel Documentation: Outputting Tables
- Laravel Documentation: Outputting Progress Bars
- Laravel Documentation: Customizing the Artisan Console
- Laravel Documentation: Testing Artisan Commands
- Laravel Code Examples: Custom Commands