Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] BindingResolutionException: Unresolvable dependency resolving #52

Closed
Anahkiasen opened this issue Jan 15, 2013 · 10 comments
Closed

Comments

@Anahkiasen
Copy link
Contributor

I'm trying to use dependency injection as explained in the docs. My controller has the following construct :

<?php
class Projects extends BaseController
{
  public function __construct(Project $projects)
  {
    $this->projects = $projects;
  }

And here is the Project model :

<?php
class Project extends Eloquent {}

In my head this was supposed to work but somehow here is the Exception that is thrown :

BindingResolutionException: Unresolvable dependency resolving [Parameter #0 [ array $attributes = Array ]].
in C:\Users\Maxime\Dropbox\WEB DESIGN\taketime-illuminate\vendor\laravel\framework\src\Illuminate\Container\Container.php line 327
at Container->getDependencies(array(object(ReflectionParameter))) in C:\Users\Maxime\Dropbox\WEB DESIGN\taketime-illuminate\vendor\laravel\framework\src\Illuminate\Container\Container.php line 301
at Container->build('Project', array()) in C:\Users\Maxime\Dropbox\WEB DESIGN\taketime-illuminate\vendor\laravel\framework\src\Illuminate\Container\Container.php line 218
at Container->make('Project', array()) in C:\Users\Maxime\Dropbox\WEB DESIGN\taketime-illuminate\vendor\laravel\framework\src\Illuminate\Foundation\Application.php line 261
at Application->make('Project') in C:\Users\Maxime\Dropbox\WEB DESIGN\taketime-illuminate\vendor\laravel\framework\src\Illuminate\Container\Container.php line 330
at Container->getDependencies(array(object(ReflectionParameter))) in C:\Users\Maxime\Dropbox\WEB DESIGN\taketime-illuminate\vendor\laravel\framework\src\Illuminate\Container\Container.php line 301
at Container->build('Projects', array()) in C:\Users\Maxime\Dropbox\WEB DESIGN\taketime-illuminate\vendor\laravel\framework\src\Illuminate\Container\Container.php line 218
at Container->make('Projects', array()) in C:\Users\Maxime\Dropbox\WEB DESIGN\taketime-illuminate\vendor\laravel\framework\src\Illuminate\Foundation\Application.php line 261
at Application->make('Projects') in C:\Users\Maxime\Dropbox\WEB DESIGN\taketime-illuminate\vendor\laravel\framework\src\Illuminate\Routing\Router.php line 573
at Router->Illuminate\Routing\{closure}()
at call_user_func_array(object(Closure), array()) in C:\Users\Maxime\Dropbox\WEB DESIGN\taketime-illuminate\vendor\laravel\framework\src\Illuminate\Routing\Route.php line 64
at Route->callCallable() in C:\Users\Maxime\Dropbox\WEB DESIGN\taketime-illuminate\vendor\laravel\framework\src\Illuminate\Routing\Route.php line 39
at Route->run(object(Request)) in C:\Users\Maxime\Dropbox\WEB DESIGN\taketime-illuminate\vendor\laravel\framework\src\Illuminate\Routing\Router.php line 658
at Router->dispatch(object(Request)) in C:\Users\Maxime\Dropbox\WEB DESIGN\taketime-illuminate\vendor\laravel\framework\src\Illuminate\Foundation\Application.php line 369
at Application->dispatch(object(Request)) in C:\Users\Maxime\Dropbox\WEB DESIGN\taketime-illuminate\vendor\laravel\framework\src\Illuminate\Foundation\Application.php line 349
at Application->run() in C:\Users\Maxime\Dropbox\WEB DESIGN\taketime-illuminate\public\index.php line 67

Can't Eloquent models be injected automatically like this ?

@ipalaus
Copy link
Contributor

ipalaus commented Jan 15, 2013

You should bind the class to $app, try adding this to routes.php:

App::bind('Project', 'Project');

or

App::bind('Project', function() { return new Project; });

@Anahkiasen
Copy link
Contributor Author

First solution didn't work (because I guess it's what Laravel does in the background by default anyway). Second one did. Thanks !
Still pretty weird though, the Model constructor doesn't have anything in particular that may be hard to create or anything, it just hydrates a Model with an empty array, no external dependency no nothing, it's weird.

@franzliedke
Copy link
Contributor

Looks like it ignores the fact that the $attributes parameter is optional. I'll take a look whether this can be fixed.

@taylorotwell
Copy link
Member

You wouldn't inject an Eloquent object like that. It wouldn't make sense. Inject a repository or something else. There would no point in injecting a model.

@Anahkiasen
Copy link
Contributor Author

Well I'm not really trying to inject an Eloquent object, I'm trying to bind Eloquent as a repository — by using its query builder methods like you would use methods from a repository to fetch from the database.

@taylorotwell
Copy link
Member

Yeah, I wouldn't suggest that. You need to write a real repository if you want to get the testability benefits.

@taylorotwell
Copy link
Member

Anyways, this isn't really a "bug" per se. The IoC container needs to know what you want to inject because there is no type hint. I can probably improve the container to inject default arguments though. I believe Dan Horrigan added a pull for that.

@Anahkiasen
Copy link
Contributor Author

What do you mean by there is not type hint ? In my controller or in the Model class ?

@taylorotwell
Copy link
Member

In the Model class.

@Anahkiasen
Copy link
Contributor Author

Oh okay :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants