Laravel:将依赖项注入扩展类

时间:2014-05-01 16:00:04

标签: php dependency-injection laravel-4

我使用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()&#39;允许父类扩展​​。

我在加载课程时遇到了错误,而且我仍然非常环保以进行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的实例,没有给出,调用   第15行和/的/var/www/app/controllers/ExtendedUserController.php   定义

如果我运行父构造函数,那么父代码的代码不应自动传递它的依赖关系,如父类中所述[&#39;码?

这不是添加依赖项的正确方法吗?任何帮助和解释将不胜感激。

1 个答案:

答案 0 :(得分:0)

我现在意识到错误,我想添加它而不是删除问题。

我在使用不同的标准搜索时发现了一个早期的stackoverflow post on this issue

我需要

  • 添加父类&#39;依赖于我的扩展类,将它们传递给我的扩展类
  • 将依赖项注入我的扩展类&#39;构造函数
  • 将这些注入的变量传递到parent::__construct()调用

这是工作代码的样子:

<?php

use Sentinel\UserController;
use Sentinel\Repo\Group\SentryGroup;
use CustomerLinks;

// Parent class' dependencies
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 ExtendedUserController extends UserController {

    protected $customerLinks;

    // Injecting both the parent class' dependencies and my new dependency into the constructor
    public function __construct(CustomerLinks $customerLinksClass,UserInterface $user, GroupInterface $group, RegisterForm $registerForm, UserForm $userForm,ResendActivationForm $resendActivationForm,ForgotPasswordForm $forgotPasswordForm,ChangePasswordForm $changePasswordForm,
SuspendUserForm $suspendUserForm){
        // passing the injected variables to the parent constructor call
        parent::__construct($user, $group, $registerForm, $userForm,$resendActivationForm,$forgotPasswordForm,$changePasswordForm,$suspendUserForm);

        // Adding my new injected class into the controller class
        $this->customerLinks = $customerLinksClass;
    }

//The rest of the class code...

这是一个相当大的脸庞时刻。希望这将有助于将来的人。

相关问题