问题
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