有没有办法执行一些工匠命令,使用自定义工匠命令,就像我想制作一个名为的自定义命令:
$ php artisan project:init
这将执行一些命令,如
php artisan migrate:refresh
和 php artisan db:seed
和 php artisan config:clear
有没有办法做到这一点?
最佳答案
有两种方法可以对命令进行分组或从另一个命令调用它。
变体 1:
在 routes/console.php
中创建新的控制台命令.
路线/控制台.php
Artisan::command('project:init', function () {
Artisan::call('migrate:refresh', []); // optional arguments
Artisan::call('db:seed');
Artisan::call('config:clear');
})->describe('Running commands');
变体 2:根据文档:https://laravel.com/docs/7.x/artisan#calling-commands-from-other-commands
使用命令行创建新命令:
$ php artisan make:command ProjectInit --command project:init
这将创建新文件:App\Console\Commands\ProjectInit
在那ProjectInit
类(class)handle
方法你可以调用另一个命令:public function handle(){
$this->call('migrate:refresh', []); // optional arguments
$this->call('db:seed');
$this->call('config:clear');
}
关于php - 自定义 Artisan 命令以执行多个命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61872499/