Support the ongoing development of Laravel.io →
posted 10 years ago
IOC

Hello

I have recently implemented the repository pattern using Jeff Ways ace tutorials at Laracast. As I am still getting to grips a bit with the architecture I'm a bit confused how to instantiate a model outside of a controller.

So current I inject a 'model' repository interface into the constructor of its controller which is then bound to the repository class via an associated service provider. This works perfectly.

Im a bit confused now as to how in a Route to access a model using this approach ( define a facade??? ).

As a final question I assume that for any additional models required by a controller, their interfaces needs to be injected into the constructor?

Thanks

Leo

Last updated 3 years ago.
0

I usually use facade, however you can alway use

App::make()
Last updated 3 years ago.
0

So using App::make() in another route or model you would use?

$lot = App::make('Acme\Repositories\Lot\LotRepositoryInterface');

I just started reading that Facades weren't ideal for testing so I was trying to avoid them!

Last updated 3 years ago.
0

leoden said:

So using App::make() in another route or model you would use?

$lot = App::make('Acme\Repositories\Lot\LotRepositoryInterface');

If a class is instantiated by the IoC container the interface can just be injected. If using a route closure you'll have to use the App::make() but in another class just inject the interface like you would in a controller.

leoden said:

I just started reading that Facades weren't ideal for testing so I was trying to avoid them!

Facades are no worse for testing than using a closure directly in the route, its best to stick to controllers. Any facade can be partially mocked with Mockery by calling FacadeName::respondsTo('something')... but its not as nice as injecting all dependencies. Injecting all controller dependencies can get very cumbersome though.

Last updated 3 years ago.
0

Thanks for the answers guys much appreciated!

Last updated 3 years ago.
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

leoden leoden Joined 13 Mar 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.

© 2025 Laravel.io - All rights reserved.