3

I am new to this concept of DI, and IoC so i might be doing this completely wrong but i am trying to inject the Model that matches a controller into that controllers constructor (UserModel -> UsersController) so that i can mock it later on.

So my model looks like:

use Illuminate\Auth\UserInterface;

class User extends Eloquent implements UserInterface {


    public function getAuthIdentifier()
    {
        return $this->getKey();
    }


    public function getAuthPassword()
    {
        return $this->password;
    }

}

And i am then trying to inject in UsersController like so :

class UsersController extends Controller {

    protected $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    public function index()
    {
        //dd( $this->user);
        $users = $this->user->all();
        foreach ( $users as $user ) 
            print_r($user);
        return;
    }

}

Then when i hit this controller in the browser i get a "Unresolvable dependency resolving" error.

I noticed that this happend only when the class that i am trying to inject is a sub class of eloquent, if i try the same code with a custom class that do not extend eloquent then it works fine.

Am i missing something?

4
  • have a look into this screencast vimeo.com/53029232 Commented Dec 10, 2012 at 19:44
  • @Raf thanks but it's actually the one that led me here, in that screen cast taylor does not show how to inject the actual model which is what i am trying to do Commented Dec 10, 2012 at 21:41
  • 1
    Did you try App::bind('UserInterface','User'); and put __controller(UserInterface $user) Commented Dec 11, 2012 at 19:15
  • Okay, I tried the scenario and I am getting the same result, an error. Commented Dec 11, 2012 at 22:26

1 Answer 1

1

Further to the comments, I finally got to know that this is a complicated issue. To bypass this you need to bind your model with the IoC and return a new instance of your model manually.

App::bind('User', function()
{
    return new User;
});
Sign up to request clarification or add additional context in comments.

1 Comment

Hey Raf, thanks i actually got that answer already from that thread : forums.laravel.com/viewtopic.php?id=3878

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.