1

I'm using the Sentinel bundle for Laravel.

The vendor class UserController has a constructor that injects a bunch of dependencies into it that looks like this:

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

I've created model class in my app called ExtendedUserController so that I can add functionality to the existing controller. My controller class looks like this currently:

<?php

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

class ExtendedUserController extends UserController {

// The rest of the class code... 

I don't have a constructor in my class because I'm letting the parent class construct when I extend it.

I have a new model that I would like to add to my controller class. My plan was to inject the dependency in a constructor for the ExtendedUserController and then call `parent::__construct()' to allow the parent class to be extended.

I'm running into errors when loading the class and I'm still green enough to laravel and dependency injection that I'm not exactly sure how to fix it or if this is even the proper way of accomplishing the task:

<?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();
    }

I'm running into the error:

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

If I run the parent constructor, shouldn't the parent's code automatically pass in it's dependencies as outlined in the parent class' code?

Is this not the proper way to add the dependency? Any help and explanation would be greatly appreciated.

1 Answer 1

1

I realize the error now and I wanted to add it in rather than deleting the question.

I found an earlier stackoverflow post on this issue when searching with different criteria.

I needed to

  • Add the parent class' dependencies to my extended class, pass them into my extended classes
  • Inject the dependencies into my extended class' constructor
  • Pass those injected variables into the parent::__construct() call

Here's what the working code looks like:

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

It was a pretty big face-palm moment. Hopefully this will help someone out in the future.

Sign up to request clarification or add additional context in comments.

1 Comment

I am running into the same problem wherein the parent class is being inherited. I would like to know doing this correct? Well technically it works but is it the correct way to do it? It looks like if I add another class MoreExtendedUserController that extends to the ExtendedUserController, I have to add the same parameters on its constructor that will be pass down to UserController. Would that be an overkill or overloading of constructor?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.