首页 文章

Laravel:将依赖项注入扩展类

提问于
浏览
1

我正在为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的实例,没有给出,在第15行的/var/www/app/controllers/ExtendedUserController.php中调用并定义

如果我运行父构造函数,父代码的代码是否应该自动传入它的依赖项,如父类的代码中所述?

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

1 回答

  • 0

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

    当我用不同的标准搜索时,我发现了早期的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...
    

    这是一个非常大的脸庞时刻 . 希望这将有助于将来的人 .

相关问题