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
I usually use facade, however you can alway use
App::make()
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!
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.
Thanks for the answers guys much appreciated!
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community