php - Laravel 5 what is the reason of binding namespaces in Service Provider? -


i read articles laravel service providers , containers. understand service provider way organize service objects bindings ioc, useful when application large.

but looked in ready service provider folder , saw appserviceprovider provider , register method if it:

public function register() {     $this->app->bind(         'illuminate\contracts\auth\registrar',         'app\services\registrar'     ); } 

why bind namespaces ioc, when can app::make anyway without binding these namespaces? thought understood how business works until saw piece of code.

why did that? thanks!

for example, u want use file storage in aplication

app::bind( 'myapp/filestorage', function(){     return new amazonfilestorage; }); 

or

app::bind( 'myapp/filestorage', 'amazonfilestorage'); 

first parameter bind method unique id bind container, second parameter callback function executed each time resolve filestorage class, can pass string representing class name.

so maybe later want use other file storage service. need change binding in application u use "myapp/filestorage"

app::bind( 'myapp/filestorage', 'systemfilestorage'); 



in case

$this->app->bind(   'illuminate\contracts\auth\registrar',   'app\services\registrar' ); 

there interface registrar :

<?php namespace illuminate\contracts\auth;  interface registrar {  /**  * validator incoming registration request.  *  * @param  array  $data  * @return \illuminate\contracts\validation\validator  */ public function validator(array $data);  /**  * create new user instance after valid registration.  *  * @param  array  $data  * @return user  */ public function create(array $data);  } 

and service registrar

<?php namespace app\services;  use app\user; use validator; use illuminate\contracts\auth\registrar registrarcontract;  class registrar implements registrarcontract {  /**  * validator incoming registration request.  *  * @param  array  $data  * @return \illuminate\contracts\validation\validator  */ public function validator(array $data) {     return validator::make($data, [         'name' => 'required|max:255',         'email' => 'required|email|max:255|unique:users',         'password' => 'required|confirmed|min:6',     ]); }  /**  * create new user instance after valid registration.  *  * @param  array  $data  * @return user  */ public function create(array $data) {     return user::create([         'name' => $data['name'],         'email' => $data['email'],         'password' => bcrypt($data['password']),     ]); }  } 

and in 'app\http\controllers\auth\authcontroller' injected

and concept behind "binding interfaces implementations" can read in official laravel 5 documantation http://laravel.com/docs/5.0/container#binding-interfaces-to-implementations , if doesn't help, ask :)


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -