Can you extend Command classes in Laravel

£可爱£侵袭症+ 提交于 2019-12-10 16:11:59

问题


I'm currently working on an application working with Laravel 4. I'm working on building a commenting system and have the basic Commands for doing Create, Update, Delete.

What I'm trying to do next is create commands for the specific objects to which the comments will be attached, for example a Blog Post.

So if my command file is called CreateCommentCommand I would like to create another command called CreatePostCommentCommand. I would like this newer class to extend the CreateCommentCommand and have the type hardcoded within.

So for CreateCommentCommand I have the following:

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;
    }

}

... and for CreatePostCommentCommand I have:

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;
    }

}

For the Handler I have CreatePostCommentCommandHandler which extends CreateCommentCommandHandler. However the problem I'm encountering is when it hits the field validators which are part of the CreateCommentHandler then it says $commentable_type was not supplied.

来源:https://stackoverflow.com/questions/29409489/can-you-extend-command-classes-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!