Support the ongoing development of Laravel.io →
IOC Packages Architecture
Last updated 1 year ago.
0

What about naming the desired class witht he full qualifyed name?

Try to replace SliderRepository by \Site\Repositories\SliderRepository.

Controllers are in the root namespace () but your class is in another namespace, so your issue seems to be normal (or I am missing something here)

Last updated 1 year ago.
0

Thanks for your fast answer atrakeur

atrakeur said: Try to replace SliderRepository by \Site\Repositories\SliderRepository.

If I do that change, it works:

public function __construct(\Site\Repositories\SliderRepository $sliderRepository){

But I wanna to use Automatic resolution http://laravel.com/docs/ioc#automatic-resolution. Indeed, with this change i don't need even a ServiceProvider.

Thank you again.

Last updated 1 year ago.
0

Get rid of the lib directory and just use Site as a child folder of app

Then use psr-4 to autoload everything in your site folder:

"psr-4": {
    "Site\\": "app/Site"
 }

then

composer dump

Your class will be found. You don't need to make an IoC binding if you just want to inject a concrete repository into the constructor. However, service providers are a useful place to bind concrete classes to an interface if you are planning to inject interfaces into the controller and have them automatically resolved.

Last updated 1 year ago.
0

As Anthony mentions above, you should autoload using the namespace, that'll simplify things for you. Also, when binding you can simply do:

$this->app->bind('SliderRepository', 'Site\Repositories\SliderRepository');

This means that later down the line you can use injection on your repository, I for example inject a validator :)

Last updated 1 year ago.
0

Heh, the first thing I had to do this morning was check this post. I posted that reply after a night of heavy drinking in Shanghai, but it looks ok. Why I came on Laravel.io in a completely drunken state is unknown to me right now :)

Anyways I'll just add if you're injecting a concrete repository there's no need to make a binding, Laravel will find it and resolve it with reflection, even if you didn't use it at top of your file. This could be done though:

$this->app->bind('Site\Repositories\SliderInterface', 'Site\Repositories\SliderRepository');

Then you can code to an interface in your controller which is always good.

Last updated 1 year ago.
0

Yes! Thanks Anthony and Ollie, I guess there is an issue in docs because there are a lot of ways to bind classes in IoC (on different versions of laravel). So, the fix was do it without the bind and inject directly with the concrete type. Now is working.

I owe you a couple of beers guys :D

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

shaggyz shaggyz Joined 22 May 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.