laravel5.4之artisan使用总结一
生活随笔
收集整理的這篇文章主要介紹了
laravel5.4之artisan使用总结一
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Artisan是laravel自帶的命令行接口:
php artisan list編寫命令
生成命令:
- 可以使用Artisan命令,
執(zhí)行完這個(gè)命令后,會(huì)在app/Console/Commands 目錄下創(chuàng)建ConsoleTest命令類。會(huì)包含默認(rèn)的屬性設(shè)置以及所有命令都共有的方法。
- 需要在ConsoleTest填寫這個(gè)類的signature和description屬性。其中的handle方法會(huì)在命令執(zhí)行時(shí)被調(diào)用。將所有命令邏輯都放在這個(gè)方法里面。
- 在編寫好命令后需要在該命令可以通過(guò)Artisan CLI執(zhí)行之前 注冊(cè)這個(gè)命令。
如下填寫這個(gè)類的signature和description屬性:
<?phpnamespace App\Console\Commands;use App\Http\Controllers\SettlementController; use Illuminate\Console\Command;class ConsoleTest extends Command {/*** The name and signature of the console command.** @var string*/protected $signature = 'console:test';/*** 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(){//TODO 需要的一些邏輯 } }?
閉包命令:
在app/Console/Kernel.php文件的comands方法中,Laravel加載了routes/console.php文件。
<?phpnamespace App\Console;use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel;class Kernel extends ConsoleKernel { /*** Register the Closure based commands for the application.** @return void*/protected function commands(){require base_path('routes/console.php');} }注冊(cè)命令:
- 在命令編寫后,需要注冊(cè)到Artisan才可以使用,需要在app/Console/Kernel.php 文件中完成。
- 在這個(gè)文件中的commands屬性中,在里面注冊(cè)命令,只需要將上面ConsoleTest所在路徑寫上,如下:
?
- 最后在終端里cd到項(xiàng)目根目錄下,執(zhí)行如下命令
會(huì)去執(zhí)行上面ConsoleTest 中的handle()方法。
?
轉(zhuǎn)載于:https://www.cnblogs.com/linst/p/8044971.html
總結(jié)
以上是生活随笔為你收集整理的laravel5.4之artisan使用总结一的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 微信小程序系列(3)如何用微信小程序写一
- 下一篇: uniapp 打包成微信小程序