[英]Command-line scripts in Laravel?
我有一些命令行腳本,我想修改它們以使用 Laravel 的功能(Eloquent 等)。
我該怎么做? 我知道 Laravel 從 index.html 文件中引導。 是否有任何用於運行命令行應用程序/腳本的規定?
php artisan make:command FancyCommand
。 在/app/Console/Commands/FancyCommand.php
找到一個受保護的變量$signature
並將其值更改為您喜歡的簽名:
protected $signature = 'fancy:command';
handle()
方法中的代碼將被執行:
public function handle() { // Use Eloquent and other Laravel features... echo 'Hello world'; }
通過將命令的類名添加到$commands
數組,在/app/Console/Kernel.php
注冊新命令。
protected $commands = [ // ... Commands\\FancyCommand::class, ];
運行你的新命令: php artisan fancy:command
。
是的,您可以使用 Artisan 命令:
Artisan::command('my:command', function () {
// Here you can use Eloquent
$user = User::find(1);
// Or execute shell commands
$output = shell_exec('./script.sh var1 var2');
});
然后使用運行它
user@ubuntu: php artisan my:command
檢查文檔: https : //laravel.com/docs/5.3/artisan
您也可以使用調度程序: https : //laravel.com/docs/5.3/scheduling
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.