【问题标题】:Laravel: injecting a dependency into an extended classLaravel:将依赖项注入扩展类
【发布时间】: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


    【解决方案1】:

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

    在使用不同条件进行搜索时,我发现了一个较早的 stackoverflow post on this issue

    我需要

    • 将父类的依赖添加到我的扩展类中,将它们传递到我的扩展类中
    • 将依赖项注入到我的扩展类的构造函数中
    • 将这些注入的变量传递到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...
    

    这是一个非常重要的时刻。希望这对将来的人有所帮助。

    【讨论】:

    • 我遇到了同样的问题,其中父类被继承。我想知道这样做正确吗?从技术上讲它可以工作,但它是正确的方法吗?看起来如果我添加另一个扩展到 ExtendedUserController 的类 MoreExtendedUserController,我必须在其构造函数上添加相同的参数,这些参数将传递给 UserController。这会是构造函数的过度杀伤或重载吗?
    猜你喜欢
    • 2016-06-22
    • 2014-06-05
    • 2022-10-15
    • 1970-01-01
    • 1970-01-01
    • 2011-12-26
    • 2018-12-18
    • 2014-02-02
    • 2017-03-10
    相关资源
    最近更新 更多