Beginner LevelLaravel
What is Artisan CLI and how do you create custom console commands?
❓ Interview Prompt: Explain Artisan CLI, php artisan make:command, command signature, handle() method, and console output formatting.
Artisan is the command-line interface included with Laravel, providing helpful commands for code generation, migrations, and caching.
Custom Commands:
Generate custom commands via 'php artisan make:command SendWeeklyDigest':
•
'$signature': Defines command name, arguments, and options ('app:send-digest {user?}'').
•
'handle()': Contains task execution logic, producing output via '$this->info()' or '$this->error()'.
Code Implementation Example
php
<?php
namespace AppConsoleCommands;
use IlluminateConsoleCommand;
class CleanOldLogsCommand extends Command
{
protected $signature = 'logs:clean {--days=30}';
protected $description = 'Clean application logs older than X days';
public function handle(): int
{
$days = (int) $this->option('days');
$this->info("Cleaning logs older than {$days} days...");
return Command::SUCCESS;
}
}💡Custom Artisan Console Command with signature options.
🎯 Interview Key Takeaway
Create custom Artisan commands to automate background maintenance tasks and CLI operations.
#Artisan#CLI#Console#Beginner
Back to all Laravel Questions→