Saturday, 15 March 2014

php - Trouble Understanding Facade in Laravel -



php - Trouble Understanding Facade in Laravel -

for part thought behind facade , how details of instantiating object hidden behind static calls.

so if take @ straight forwards illustration here: example

we see code facade, @ point sets sort of alias. @ point seems facade still knows nil superuser class.

class superuserfacade extends facade { protected static function getfacadeaccessor() { homecoming 'myalias'; } }

the logic glued service provider here seems:

class superuserserviceprovider extends serviceprovider { public function register() { app::bind('myalias', function(){ homecoming new superuser; }); } }

but binds class facade alias myalias. why bother facade class , 2 separate files, can't logic right in service provider? or alternatively in facade provider having homecoming superuser class?

it seems have facade not doing , file telling facade do. why have these concerns been separated?

the facade class simple proxy -- directs calls facade class root class, retrieved ioc container via facade accessor (in case, myalias).

i phone call superuser::whoami()

the superuser facade goes "okay, need find class i'm proxy for."

the facade calls getfacadeaccessor() determine ioc binding retrieve , subsequently call.

the facade requests myalias key ioc container. container returns existing class if has been built already, or runs bound closure generates new superuser instance.

now facade knows class it's passing calls to, forwards whoami() method phone call newly-returned superuser instance, returns whatever designed to.

the service provider's register() method registers binding ioc container, retrieved later whatever needs it. that's it. myalias simple string key used binding.

the facade allows utilize ioc-bound class if static class.

i recommend reading other articles concept, because article linked both inaccurate , not informational why things work. here improve article chris fidao.

php laravel laravel-4 facade

No comments:

Post a Comment