【发布时间】:2014-05-01 16:00:04
【问题描述】:
我正在为 Laravel 使用 Sentinel 包。
供应商类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();
}
我遇到了错误:
传递给 Sentinel\UserController::__construct() 的参数 1 必须是 Sentinel\Repo\User\UserInterface 的实例,没有给出,调用 /var/www/app/controllers/ExtendedUserController.php 在第 15 行和 定义
如果我运行父构造函数,父代码不应该自动传递它的依赖项,如父类代码中所述吗?
这不是添加依赖项的正确方法吗?任何帮助和解释将不胜感激。
【问题讨论】:
标签: php dependency-injection laravel-4