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