Laravel定制工匠命令未列出

问题描述 投票:1回答:3

我写了几个工匠命令。 所有这些都有共同的功能,所以我写了一个Command类,而不是扩展MyBaseCommand类,所以所有命令都扩展了这个:

namespace App\Console\Commands;
use Illuminate\Console\Command;

class SomeCommand extends MyBaseCommand
{
    protected $signature = 'mycommands:command1';

    protected $description = 'Some description';

    :
    :

和基类:

namespace App\Console\Commands;

class MyBaseCommand extends Command
{
    :
    :

问题在于,由于某些原因,这些命令不再与php artisan一起列出。

任何想法我怎么能强制laravel列出这些命令?

laravel artisan
3个回答
3
投票
protected $signature = 'mycommands:command1'; //this is your command name

打开app\Console\kernel.php文件。

protected $commands = [
    \App\Console\Commands\SomeCommand::class,
]

然后运行

php artisan list

0
投票

Laravel尝试自动为您注册命令:

/**
 * Register the commands for the application.
 *
 * @return void
 */
protected function commands()
{
    $this->load(__DIR__.'/Commands');

    require base_path('routes/console.php');
}

你可以在App\Console\Kernel.php找到这个

确保您的班级有signaturedescription属性。

enter image description here

enter image description here


0
投票

这是非常愚蠢的,无论如何,因为它可能发生在我离开这里的其他人的答案:

我想隐藏基类,所以我在其中包含了这一行:

protected $hidden = true;

当然,这个变量的值传播到了高级类,这使隐藏了自定义命令。

解决方案就是将这一行添加到这些文件中:

protected $hidden = false;

======================更新======================

正如@ aken-roberts所提到的,更好的解决方案就是简单地使基类抽象化:

namespace App\Console\Commands;

abstract class MyBaseCommand extends Command
{

    abstract public function handle();

    :
    :

在这种情况下,工匠不会列出它,也不能执行。

© www.soinside.com 2019 - 2024. All rights reserved.