Laravel : Up and Running 閱讀筆記 Chapter 7

guitarbien
Feb 25, 2017 · 2 min read

Artisan

是 symfony console component 的上層實作,可以用來處理一些 Laravel 的常用需求:開一個新的 Controller 檔案、設定排成功能、重新建立專案所需的資料表,甚至暫時關閉網站等等。也可以自己寫一個 Artisan 的新功能。

Artisan 本身是個 php 檔案,經由提供的 sub command 來對應到需求的 class 和 method。

php artisan make:console WelcomeNewUser --command=email:newusers

這樣的指令會在 App\Console\Commands 下產生一個 WelcomeNewUser.php 的檔案,並且將主要的實作寫在 handle() method 中。而此 command 也必須被註冊在 app/Console/Kernel.php 中。


參數(Arguments)定義:

  • 必填:password:reset {userId}
  • 選填:password:reset {userId?}
  • 預設值:password:reset {userId=1}

選項(Options)定義:

  • 旗標類型,不用帶值:password:reset {userId} {--sendEmail}
  • 要帶值:password:reset {userId} {--password=}
  • 有提供預設值:password:reset {userId} {--queue=default}

也都可以用多筆的方式作為陣列傳入,例如指令下: php artisan password:reset 1 2 3 或是 php artisan password:reset --ids=1 --ids=2 --ids=3,參數或選項的定義則是 {userIds*} 或是 {--ids=*}

由於陣列接參數可以一直用空白做區隔往後疊加,所以應該要放在最後面才能辨識。

還可使用一些 method 來跟 user 做互動,例如提供選項、或是請 user 輸入密碼等等。

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade