Laravel 4: Confused about how to use App :: make ()

I am trying to execute the repository template described in this article http://code.tutsplus.com/tutorials/the-repository-design-pattern--net-35804#highlighter_174798 And I am trying to create an instance of the class in Laravel using App :: make () (I think this is a Laravel factory template?), And I'm trying to parse the arguments for my class, but I cannot figure out how to do this.

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 someone give an example of using App :: make () or should I go in the completely wrong direction and should not use App :: make ()?

+6
source share
2 answers

Good people in the Laravel forum answered me this http://laravel.io/forum/02-10-2014-laravel-4-confused-about-how-to-use-appmake

To a large extent, if you can link your own instance code with App :: bind (); So

 App::bind('My\NewClass', function() use ($classArgs) { return new My\NewClass($classArgs['id'], $classArgs['title']); }); // get the binding $newClass = App::make('My\NewClass'); 
+4
source

The application is actually a facade for the Laravel IoC container, which is commonly used for automatic resolution. Understanding the concept of IoC is vital for integrated application development, but small projects will likely benefit from a well-designed architecture. I would recommend first diving into the Laravel documentation and trying out a few examples for service providers, bindings, and automatic resolution.

Speaking of your example:

 namespace My; class NewClass { function __construct($id, $title) { $this->id = $id; $this->title = $title; } } $newClass = App::make('My\NewClass', [1, 'test']); 
+7
source

All Articles