2014-05-01 66 views
0

我使用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

如果我運行父類的構造,不應家長的代碼會自動傳遞它的依賴作爲父類的代碼概括?

這不是添加依賴關係的正確方法嗎?任何幫助和解釋將不勝感激。

回答

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... 

這是一個非常大的臉掌時刻。希望這會在未來幫助別人。

+0

我遇到了繼承父類的相同問題。我想知道這樣做是正確的嗎?從技術上講,它的工作原理是正確的方法嗎?它看起來像是如果我添加擴展到ExtendedUserController的另一個類MoreExtendedUserController,我必須在它的構造函數上添加相同的參數,並傳遞給UserController。這會是一個矯枉過正或重載的構造函數? – basagabi