无法通过传递的输入深度扩展Laravel Artisan Command [英] Unable to deeply extend Laravel Artisan Command with input passed down

查看:253
本文介绍了无法通过传递的输入深度扩展Laravel Artisan Command的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试创建基本命令,然后对其进行扩展.提供给扩展命令的输入不会使它陷入困境.

Attempting to create a base command, and then extend it. Input provided to the extending command doesn't make it down the tree.

BaseApiClassBuilder.php

use Illuminate\Console\Command;

class BaseApiClassBuilder extends Command
{
    // rest of class follows...
}

MakeApiCollection.php

class MakeApiCollection extends BaseApiClassBuilder
{

    protected $signature = 'make:apicollection {name} {--namespace=}';

    protected $description = 'Make an API Collection/Resource';

    // guts of class...
}

我正在运行控制台命令

artisan make:apicollection testApiCollection

已收到控制台错误:

在"App \ Console \ Commands \ BaseApiClassMaker"中定义的命令不能 名称为空.

The command defined in "App\Console\Commands\BaseApiClassMaker" cannot have an empty name.

可以在Laravel中扩展Command类的类似问题,但是那个有点过时了,不够具体,也没有答案.

Similar question to Can you extend Command classes in Laravel but that one is a bit out of date and less specific, and also unanswered.

仅当我扩展基本命令而不是"command"时才会发生错误.

The error only occurs when I extend my base command, instead of 'command'.

我确定在构造函数中未调用验证,因此弄清楚输入在何处进行验证以及为什么没有完全验证输入实际上是非常棘手的.

I've determined that the validation isn't called in the constructor, it's actually quite tricky to figure out where the input is being validated, and why it isn't making it all the way down.

毫无疑问,我正在做一些愚蠢的事情,并且有一个简单的解决方案...但是我找不到它!

No doubt I'm doing something goofy and there's an easy solution... but I can't find it!

任何人都可以帮助我更好地理解这一点,非常感谢收到的评论/答案/反馈.如果我完全以错误的方式处理此问题,也请告诉我.我确实注意到我可以扩展GeneratorCommand,它具有我需要的许多帮助程序,但似乎并不能解决这个问题.

I anyone can help me understand this better, comments/answers/feedback very gratefully received. If I'm approaching this completely the wrong way, let me know too. I did notice I could have extended GeneratorCommand instead which has many of the helpers I need, but doesn't seem to solve this problem.

推荐答案

通过使父类成为抽象,您无需定义signature

By making the parent class abstract you don't need to define the signature

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

abstract class BaseCommand extends Command
{
    ...
}

稍后在子类中,根据需要设置signature

Later in child class you set the signature as needed

class ChildCommand extends BaseCommand
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'make:apicollection {specific-name} {--namespace=}';

这篇关于无法通过传递的输入深度扩展Laravel Artisan Command的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆