本文文檔適用於laravel5.2版本,其他版本參考。
artisan 命令查詢
php artisan list
artisan 幫助
php artisan help
有沒想過自己寫一些命令,然后通過artisan執行呢,根據文檔,我們來一一操作
創建
php artisan make:console SendEmails
#帶命令寫入
php artisan make:console SendEmails --command=emails:send
這個時候生成:app\Console\Commands\SendEmails.php文件
解析文件
namespace App\Console\Commands; use Illuminate\Console\Command; class SendEmails extends Command { /** * The name and signature of the console command. *控制台的命令指令,exp:php artisan emails:seed * @var string */ protected $signature = 'emails:send'; /** * The console command description. *描述 * @var string */ protected $description = 'Command description'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. *執行命令 * @return mixed */ public function handle() { } }
傳值
#必須傳值 protected $signature = 'email:send {user}'; php artisan email:send 1(必須有這個,否則報錯) #可有可無傳值 protected $signature = 'email:send {user?}'; php artisan email:send 1(可有可無) #默認值 protected $signature = 'email:send {user=1024}'; php artisan email:send 1(可有可無)
開關傳遞
開關:表示一開一關,換成返回未:1/0,true/false
protected $signature = 'email:send {user} {--queue}'; // --queue這個名字可以隨便取,避免認為是隊列, //如果--queue開關被傳遞,其值是true,否則其值是false: php artisan email:send 1 --queue
開關分配值
//選項值被用戶通過=來分配: protected $signature = 'email:send {user} {--queue=}'; php artisan email:send 1 --queue='on'
選項別名
email:send {user} {--Q|queue}
定義參數和選項以便指定輸入數組
這沒看懂,不知道在控制台怎么傳值
email:send {user*} email:send {user} {--id=*}
獲得傳值
argument和option
// 獲取指定選項... $queueName = $this->option('queue'); // 獲取所有選項... $options = $this->option()
輸入提示
/** * 執行控制台命令 * * @return mixed */ public function handle(){ $name = $this->ask('What is your name?'); $password = $this->secret('What is the password?');//隱藏 } //用戶確認 if ($this->confirm('Do you wish to continue? [y|N]')) { // }
索引選擇
$name = $this->choice('What is your name?', ['Taylor', 'Dayle'], false); 選擇,0,1,...
輸出,反饋信息
line,info, comment, question 和 error方法
//要顯示一條錯誤消息,使用error方法。錯誤消息文本通常是紅色: $this->error('Something went wrong!'); //如果你想要顯示原生輸出,可以使用line方法,該方法輸出的字符不帶顏色: $this->line('Display this on the screen'); //表格布局 $headers = ['Name', 'Email']; $users = App\User::all(['name', 'email'])->toArray(); $this->table($headers, $users);
進度條
$users = App\User::all(); $this->output->progressStart(count($users)); foreach ($users as $user) { $this->performTask($user); $this->output->progressAdvance(); } $this->output->progressFinish();
更多查看(http://symfony.com/doc/2.7/components/console/helpers/progressbar.html)
注冊命令
app/Console/Kernel.php 把這個文件導入,
protected $commands = [ 'App\Console\Commands\SendEmails' ];`
CLI之外執行Artisan命令
Route::get('/foo', function () { $exitCode = Artisan::call('email:send', [ 'user' => 1, '--queue' => 'default' ]); }); //需要指定不接收字符串的選項值 $exitCode = Artisan::call('migrate:refresh', [ '--force' => true, ]);
使用Artisan上的queue隊列
Route::get('/foo', function () { Artisan::queue('email:send', [ 'user' => 1, '--queue' => 'default' ]); });
通過其他命令調用命令
已存在的Artisan命令中調用其它命令
/** * 執行控制台命令 * * @return mixed */ public function handle(){ $this->call('email:send', [ 'user' => 1, '--queue' => 'default' ]); }
調用其它控制台命令並阻止其所有輸出
$this->callSilent('email:send', [ 'user' => 1, '--queue' => 'default' ]);
from:https://www.jianshu.com/p/a880a494d122
