Artisan Console
PHP:7.2
Laravel:5.8
Artisan 是 Laravel 提供的命令列工具,底層是由 Symfony/Console 實作,對於一些環境建設或是資料 migrate 都是不可或缺的小幫手。
除了方便之外,最重要的是,除了原本的指令,我們也可以自行開發指令來完成我們的需求。
Basic Usage
# 列出 artisan 的指令清單
php artisan listWriting Commands
要建立屬於自己的 Command,也是透過 artisan 指令來完成。
建立 SendEmails Command
php artisan make:command SendEmails建立完成以後,就可以在新產生的檔案中進行需要的修改。
app/Console/Commands/SendEmails.php 基本結構
namespace App\Console\Commands;
use Illuminate\Console\Command;
class SendEmails extends Command
{
    // 指令名稱
    protected $signature = 'command:name';
    // Command 說明
    protected $description = 'Command description';
    public function __construct()
    {
        parent::__construct();
    }
    public function handle()
    {
        // 邏輯寫在此
    }
}完成之後,只要就可以直接使用 php artisan command:name 便可以執行自訂的 Command 了。
Parameters
除了基本的使用,Command 也可以搭配許多參數配合使用。
設定參數
// 自訂 argument
// php artisan send:email johnsonlu
protected $signature = 'send:email {user}';
// Optional argument
protected $signature = 'send:email {user?}';
// Optional argument 並設定初始值
protected $signature = 'send:email {user=foo}';
// Argument array
// php artisan send:email johnsonlu yolandalin
protected $signature = 'send:email {user*}';
// 自訂 options,queue 不能傳入值,只能當 switch 使用
// php artisan send:email johnsonlu --queue
protected $signature = 'send:email {user} {--queue}';
// 自訂 options 並設定初始值(可傳入值)
// php artisan send:email johnsonlu --queue="TEST"
protected $signature = 'send:email {user} {--queue=default}';
// Options 簡寫
// php artisan send:email johnsonlu -Q="TEST"
protected $signature = 'send:email {user} {--Q|queue}';
// Options array
// php artisan send:email johnsonlu --queue="TEST" --queue="TEST2"
protected $signature = 'send:email {user} {--queue=*}';
// 為參數加上說明
protected $signature = 'send:email
                        {user : The ID of the user}
                        {--queue= : Whether the job should be queued}';取得參數
public function handle()
{
    // 取得特定argument
    $value = $this->argument('name');
    // 取得所有argument
    $arguments = $this->argument();
    // 取得特定option
    $value = $this->option('queue');
    // 取得所有option
    $options = $this->option();
}Input and Output
另外,指令執行後的輸出也很重要,可以透過 Command 提供的 methods 來進行輸出。
public function handle()
{
    // 顯示用 method
    $this->info('Display this on the screen');
    $this->comment('Hello');
    $this->question('???');
    $this->error('Something went wrong!');
    $this->line('Display this on the screen');
    // Input
    $name = $this->ask('What's your name);
    // Input Secret
    $password = $this->secret('What is the password?');
    // Confirm
    if ($this->confirm('Do you wish to continue?')) {
        // do something
    }
    // 輸入時有 autocomplete 功能
    $name = $this->anticipate('What is your name?', ['Taylor', 'Johnson']);
    // Choice question
    $defaultIndex = 0;
    $name = $this->choice('What is your name?', ['Taylor', 'Dayle'], $defaultIndex);
}Use Artisan in Code Level
Artisan 也可以在 code level 中呼叫。
Controller Example
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;
class TestController extends Controller
{
    public function test(Request $request)
    {
        Artisan::call('send:mail', [
            // Argument
            'name' => 'johnsonlu',
            // Option
            '--queue' => 'test1'
        ]);
    }
}