简体   繁体   English

未列出 Laravel 自定义工匠命令

[英]Laravel custom artisan command not listed

I have several artisan commands which I wrote.我有几个我写的工匠命令。
All of them share common functionality, so instead of extending Command class, I wrote a MyBaseCommand class so all commands extend this one:它们都有共同的功能,所以我没有扩展Command类,而是编写了一个MyBaseCommand类,因此所有命令都扩展了这个类:

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

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

    protected $description = 'Some description';

    :
    :

And the base class:和基类:

namespace App\Console\Commands;

class MyBaseCommand extends Command
{
    :
    :

The problem is that from some reason these commands are no longer listed with php artisan .问题是由于某些原因,这些命令不再与php artisan一起列出。

Any idea how can I force laravel to list these commands as well?知道如何强制 Laravel 也列出这些命令吗?

protected $signature = 'mycommands:command1'; //this is your command name

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

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

then run然后运行

php artisan list

Laravel tries to register the commands for you automatically with: Laravel 尝试自动为你注册命令:

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

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

You can find this in the App\Console\Kernel.php您可以在App\Console\Kernel.php中找到它

Make sure that your classes have a signature and description property.确保您的类具有signaturedescription属性。

在此处输入图像描述

在此处输入图像描述

It is quite stupid, anyway since it might happen to someone else I leave here the answer:这很愚蠢,无论如何,因为它可能发生在其他人身上,所以我把答案留在这里:

I wanted to hide the base class, so I had inside it this line:我想隐藏基类,所以我在里面有这一行:

protected $hidden = true;

Of-course, the value of this variable was propagated to the high-level class, what made the custom commands hidden.当然,这个变量的值被传播到高级类,隐藏了自定义命令。

The solution is simply to add to these files this line:解决方案只是将这一行添加到这些文件中:

protected $hidden = false;

====================== UPDATE ====================== ======================更新======================

As @aken-roberts mentions, a better solution is simply making the base class abstract:正如@aken-roberts 提到的,更好的解决方案是简单地使基类抽象:

namespace App\Console\Commands;

abstract class MyBaseCommand extends Command
{

    abstract public function handle();

    :
    :

In this case artisan doesn't list it, and it cannot be executed.在这种情况下 artisan 没有列出它,它不能被执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM