The make method accepts the name of the class or interface you wish to resolve: $api = $this->app->make('HelpSpot\API'); One advantage to using the make method, is that Laravel will automatically inject any dependencies the class may define in it's constructor.
If the binding is registered as shared (which means as singleton), the class instance is returned, otherwise a new instance every time. BTW, Container::singleton is the same as Container::bind with the third parameter set to true.
Since you define a custom namespace libraries\data , the application will try to find the App class in the libraries\data namespace. If you want to use the laravel App class you need to write this: $config = \App::make('config'); Or add use App; at the top of your file like you did with the DB class.
The Laravel service container is a powerful tool for managing class dependencies and performing dependency injection. Dependency injection is a fancy phrase that essentially means this: class dependencies are "injected" into the class via the constructor or, in some cases, "setter" methods.
I am trying to follow the repository pattern outlined in this article http://code.tutsplus.com/tutorials/the-repository-design-pattern--net-35804#highlighter_174798 And I am trying to instantiate a class in Laravel using App::make() (Which I am guessing is Laravel's factory pattern?) and I am trying to parse arguments to my class but I can't work out how to do it.
Code:
namespace My;
class NewClass {
function __construct($id, $title)
{
$this->id = $id;
$this->title = $title;
}
}
$classArgs = [
'id' => 1,
'title' => 'test',
]
$newClass = App::make('My\NewClass', $classArgs);
Can anyone point to an example of how to use App::make() or have I gone in the completely wrong direction and shouldn't be using App::make()?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With