我使用Laravel的Sentinel捆綁包。Laravel:向擴展類注入依賴關係
廠商類UserController
有注入一堆依賴關係到它的構造,看起來像這樣:
<?php namespace Sentinel;
use Sentinel\Repo\User\UserInterface;
use Sentinel\Repo\Group\GroupInterface;
use Sentinel\Service\Form\Register\RegisterForm;
use Sentinel\Service\Form\User\UserForm;
use Sentinel\Service\Form\ResendActivation\ResendActivationForm;
use Sentinel\Service\Form\ForgotPassword\ForgotPasswordForm;
use Sentinel\Service\Form\ChangePassword\ChangePasswordForm;
use Sentinel\Service\Form\SuspendUser\SuspendUserForm;
use BaseController, View, Input, Event, Redirect, Session, Config;
class UserController extends BaseController {
protected $user;
protected $group;
protected $registerForm;
protected $userForm;
protected $resendActivationForm;
protected $forgotPasswordForm;
protected $changePasswordForm;
protected $suspendUserForm;
/**
* Instantiate a new UserController
*/
public function __construct(
UserInterface $user,
GroupInterface $group,
RegisterForm $registerForm,
UserForm $userForm,
ResendActivationForm $resendActivationForm,
ForgotPasswordForm $forgotPasswordForm,
ChangePasswordForm $changePasswordForm,
SuspendUserForm $suspendUserForm)
{
$this->user = $user;
$this->group = $group;
$this->registerForm = $registerForm;
$this->userForm = $userForm;
$this->resendActivationForm = $resendActivationForm;
$this->forgotPasswordForm = $forgotPasswordForm;
$this->changePasswordForm = $changePasswordForm;
$this->suspendUserForm = $suspendUserForm;
//Check CSRF token on POST
$this->beforeFilter('Sentinel\csrf', array('on' => array('post', 'put', 'delete')));
// Set up Auth Filters
$this->beforeFilter('Sentinel\auth', array('only' => array('change')));
$this->beforeFilter('Sentinel\inGroup:Admins', array('only' => array('show', 'index', 'create', 'destroy', 'suspend', 'unsuspend', 'ban', 'unban', 'edit', 'update')));
//array('except' => array('create', 'store', 'activate', 'resend', 'forgot', 'reset')));
}
// The rest of the class code...
我已經創建模型類在我的應用程序稱爲ExtendedUserController
,這樣我可以給添加功能現有控制器。我的控制器類看起來像這樣目前:
<?php
use Sentinel\UserController;
use Sentinel\Repo\Group\SentryGroup;
class ExtendedUserController extends UserController {
// The rest of the class code...
我沒有一個構造函數在我的課,因爲我讓父類的構造時,我對它進行擴展。
我有一個新的模型,我想添加到我的控制器類。我的計劃是在ExtendedUserController
的構造函數中注入依賴項,然後調用`parent :: __ construct()'以允許擴展父類。
我在加載類時遇到了錯誤,而且我仍然足夠的綠色以便進行laravel和依賴注入,我不確定如何修復它,或者如果這是完成任務的正確方法:
<?php
use Sentinel\UserController;
use Sentinel\Repo\Group\SentryGroup;
use CustomerLinks;
class ExtendedUserController extends UserController {
protected $customerLinks;
public function __construct(CustomerLinks $customerLinksClass){
$this->customerLinks = $customerLinksClass;
parent::__construct();
}
我遇到的錯誤:
Argument 1 passed to Sentinel\UserController::__construct() must be an instance of Sentinel\Repo\User\UserInterface, none given, called in /var/www/app/controllers/ExtendedUserController.php on line 15 and defined
如果我運行父類的構造,不應家長的代碼會自動傳遞它的依賴作爲父類的代碼概括?
這不是添加依賴關係的正確方法嗎?任何幫助和解釋將不勝感激。
我遇到了繼承父類的相同問題。我想知道這樣做是正確的嗎?從技術上講,它的工作原理是正確的方法嗎?它看起來像是如果我添加擴展到ExtendedUserController的另一個類MoreExtendedUserController,我必須在它的構造函數上添加相同的參數,並傳遞給UserController。這會是一個矯枉過正或重載的構造函數? – basagabi