Laravel 4 您可以在Laravel中扩展命令类吗

Laravel 4 您可以在Laravel中扩展命令类吗,laravel-4,command,Laravel 4,Command,我目前正在使用Laravel 4开发一个应用程序。我正在构建一个评论系统,拥有创建、更新、删除的基本命令 我接下来要做的是为注释将附加到的特定对象创建命令,例如博客文章 因此,如果我的命令文件名为CreateCommentCommand,我想创建另一个名为CreatePostCommentCommand的命令。我希望这个较新的类扩展CreateCommentCommand,并在其中硬编码类型 因此,对于CreateCommentCommand我有以下内容: class CreateComment

我目前正在使用Laravel 4开发一个应用程序。我正在构建一个评论系统,拥有创建、更新、删除的基本命令

我接下来要做的是为注释将附加到的特定对象创建命令,例如博客文章

因此,如果我的命令文件名为
CreateCommentCommand
,我想创建另一个名为
CreatePostCommentCommand
的命令。我希望这个较新的类扩展
CreateCommentCommand
,并在其中硬编码
类型

因此,对于
CreateCommentCommand
我有以下内容:

class CreateCommentCommand extends Command {

    public $commentable_type;
    public $commentable_id;
    public $comment;
    public $user_id;

    public function __construct($commentable_type = null,
                                $commentable_id = null,
                                $comment = null,
                                $user_id = null)
    {
        $this->commentable_type = $commentable_type;
        $this->commentable_id = $commentable_id;
        $this->comment = $comment;
        $this->user_id = $user_id;
    }

}
。。。对于
CreatePostCommentCommand
我有:

class CreatePostCommentCommand extends CreateCommentCommand {

    public $commentable_type = 'post';

    public function __construct($commentable_id = null,
                                $comment = null,
                                $user_id = null)
    {
        $this->commentable_id = $commentable_id;
        $this->comment = $comment;
        $this->user_id = $user_id;
    }

}

对于处理程序,我有
CreatePostCommentCommandHandler
,它扩展了
CreateCommentCommandHandler
。然而,我遇到的问题是,当它击中属于
CreateComentHandler
的字段验证程序时,它会说
$commentable\u type
未提供。

请向我们显示导致问题的字段验证程序代码
$command
在到达字段之前就丢失了字段,因此这不是问题这个问题。更重要的是,
CreateCommentCommandHandler
的底层
handle
函数不知道
commentable\u类型
是由子类设置的。但是,如果没有导致错误的代码,就很难帮助您…好奇,您是否能正常工作?我希望创建一些类似的东西,在这里我将为扩展Command类的类定义before()和after()函数(为在common before方法中为所有命令运行的所有命令实现简单的数据库日志记录)。然后有我自己的命令来扩展这个类(是否会像运行make:command一样简单,然后像上面的示例一样修改“extends命令”以扩展“CustomCommand”…或者您仍然无法实现这一点?我目前正在考虑通过使用Trait解决其中的一些问题…但它没有像扩展构造函数那样工作。