1
我正在使用最近的存储库,我试图解决一些默认操作或主要存储库中名为AbstractRepository
的行为。使用Laravel容器作为存储库
的AbstractRepository
看起来是这样的:
class AbstractRepository
{
protected $session;
protected $loggedUser;
public function __construct(Session $session)
{
$this->session = $session->current();
$this->loggedUser = $session->currentUser();
}
}
在每一个存储库我希望能够利用这些特性,但我必须调用parent::__construct()
在每一个存储库来执行的构造。
有什么办法可以让laravel的容器处理这个,而不是调用每个存储库中的父构造函数吗?
所以我可以做这样的:
class CommentRepository extends AbstractRepository implements ICommentRepository
{
public function like($commentId)
{
$entry = Like::where('comment_id', $commentId)->where('user_id', $this->loggedUser->id);
}
}