我能够以编程方式注册事件,Illuminate\Support\Facades\Event及其侦听器方法。我想以类似的方式动态注册命令。在拉拉维尔有办法吗?或者,除了在app/Console/Kernel.php中注册它之外,在Laravel中做什么最好的方法呢?
更新我可以通过下面的代码注册一个类。
use Illuminate\Console\Application as Artisan;
if (app()->runningInConsole()) {
Artisan::starting(function ($artisan) use ($commandClass) {
$artisan->resolveCommands($commandClass);
});
}发布于 2019-05-24 04:11:20
虽然Pablo提供的方法可能是单个目录的更好选择,但是如果命令分布在不同的名称空间和目录中,最终可能会在app/控制台/Kernel.php中添加多个条目。
在我的用例中,$commandClass是从跨多个编写器包的多个xml文件中提取的,因此,我不得不使用以下方法:
use Illuminate\Console\Application as Artisan;
// fetch command classes from different sources and register it here.
if (app()->runningInConsole()) {
Artisan::starting(function ($artisan) use ($commandClass) {
$artisan->resolveCommands($commandClass);
});
}发布于 2019-05-20 01:30:10
如果您查看您的app/Console/Kernel.php,您应该会看到这样的语句:
$this->load(__DIR__.'/Commands');这意味着保存在app/Console/Commands/中的所有命令类都将自动加载和注册。此外,如果使用artisan,Ex:php artisan make:command MyCommand创建命令,类将存储在app/Console/Commands/MyCommand.php中。
https://stackoverflow.com/questions/56213293
复制相似问题