php - How do I access the getServiceLocator in Model ZF2 -
i trying access getservicelocator function in model. works in controller when move model null when trying access.
call member function get() on null
it seemed below link offered comparable solution had trouble implementing
use module in our custom helper in zend framework 2
below code trying run in model.
use zend\servicemanager\servicelocatorawareinterface; use zend\servicemanager\servicelocatorinterface; class filemanager implements servicelocatorawareinterface { somefunction(){ $thumbnailer = $this->getservicelocator()->get('webinoimagethumb'); } }
thank m
when @ possible, should not try access servicelocator inside class except factory. main reason if servicelocator injected class, have no idea class's dependencies are, because potentially contain anything.
with regard dependency injection, have 2 basic choices: constructor or setter injection. rule of thumb, prefer constructor injection. setter injection should used optional dependencies, , makes code more ambiguous, because class mutable. if use purely constructor injection, dependencies immutable, , can there.
with view helpers, use __invoke
instead of __construct
inject dependencies.
see https://stackoverflow.com/a/18866169/1312094 description of how set class implement factoryinterface
Comments
Post a Comment