php - Laravel 4 setting up model using the IoC container -


i watched video , wanted change laravel controllers had dependencies managed laravel's ioc container. video talks creating interface model , implementing interface specific data source used.

my question is: when implementing interface class extends eloquent , binding class controller accessible $this->model, should create interfaces , implementations eloquent models may returned when calling methods such $this->model->find($id)? should there different classes model , modelrepository?

put way: how do new model when model in $this->model.

generally, yes, people doing pattern (the repository pattern) have interface have methods defined app use:

interface somethinginterface {      public function find($id);      public function all();      public function paged($offset, $limit);  } 

then create implementation of this. if you're using eloquent, can make eloquent implementation

use illuminate\database\model;  class eloquentsomething {      protected $something;      public function __construct(model $something)     {         $this->something = $something;     }      public function find($id)     {         return $this->something->find($id);     }      public function all() { ... }      public function paged($offset, $limit) { ... }  } 

then make service provider put together, , add app/config/app.php.

use something; // eloquent model use namespace\path\to\eloquentsomething; use illuminate\support\serviceprovider;  class reposerviceprovider extends serviceprovider {      public function register()     {         $app = $this->app;          $app->bind('namespace/path/to/somethinginterface', function()         {             return new eloquentsomething( new );         });     }  } 

finally, controller can use interface type hint:

use namespace/path/to/somethinginterface;  class somethingcontroller extends basecontroller {      protected $something;      public function __construct(somethinginterface $something)     {          $this->something = $something;     }       public function home() { return $this->something->paged(0, 10); }  } 

that should it. apologies on errors, isn't tested, lot.

downsides:

more code :d

upsides:

  • able switch out implementations (instead of eloquentsomething, can use arraysomething, mongosomething, whatever), without changing controller code or code uses implementation of interface.
  • testable - can mock eloquent class , test repository, or mock constructor dependency , test controller
  • re-usable - can app::make() concrete eloquentsomething anywhere in app , re-use repository anywhere in code
  • repository place add additional logic, layer of cacheing, or validation rules. stock mucking in controllers.

finally:, since typed out , still didn't answer question (wtf?!), can new instance of model using $this->model. here's example creating new something:

// interface: public function create(array $data);  // eloquentsomething: public function create(array $data)  {     $something = this->something->newinstance();     // continue on creation logic } 

key method, newinstance().


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -