-- Maxim Savenko <[email protected]> wrote
(on Monday, 17 September 2012, 03:25 PM +0400):
> Is there are any way to get ServiceLocator or other DI manager in other
> classes, Model classes for example?
Yes -- implement Zend\ServiceManager\ServiceLocatorAwareInterface, and
then define a service for your model somewhere in your configuration,
and make sure you pull your model from the service locator. What I mean
is:
class SomeModel implements ServiceLocatorAwareInterface
{
protected $services;
public function setServiceLocator(ServiceLocatorInterface $locator)
{
$this->services = $locator;
}
public function getServiceLocator()
{
return $this->services;
}
}
>From there, make sure SomeModel is in your service configuration, and
that you fetch it from the SM instance.
That said, I recommend not doing this. Use the ServiceManager for
Inversion of Control. By this I mean: have a factory for the model that
injects dependencies into the newly created instance, and then inject
the model where you need it.
As an example:
'service_manager' => array('factories' => array(
'SomeModel' => function ($services) {
$db = $services->get('Zend\Db\Adapter\Adapter');
$inputFilter = $services->get('Some\InputFilter');
$model = new SomeModel();
$model->setDbAdapter($db);
$model->setInputFilter($inputFilter);
return $model;
},
)),
'controllers' => array('factories' => array(
'SomeController' => function ($controllers) {
$services = $controllers->getServiceLocator();
$model = $services->get('SomeModel');
$controller = new Some\Controller();
$controller->setModel($model);
return $controller;
},
)),
This approach allows you to easily substitute different implementations
when necessary. Additionally, it provides you with an explicit
documentation of what dependencies a given class has. These two factors
combined will help make your code more testable, and more easily
extended and maintained.
> I read "Creating a ServiceManager-aware
> class"<http://zf2.readthedocs.org/en/latest/modules/zend.service-manager.quick-start.html>,
> It's fine, but it works only with controllers.
> It'd be fine if this will work with other classes instantiated with
> ServiceLocator/Manager. for example:
>
> I have some configured invocable class: myClass described as follow:
>
> class myClass implements ServiceManagerAwareInterface {
> private $serviceManager=null;
>
> ...
> }
>
> , in controller i use serviceLocator to get it
>
> $myClassObj = $serviceLocator->get('myClass')
>
> ,so after getting $myClassObj it will have instance of $serviceManager
> which in it's turn can use it to get other resources.
>
>
> And another question. Is there are any way to pass parameters to resources
> described in Module.php when they are initialized? I mean while i do
> something like
> $myClassObj = $serviceLocator->get('myClass' , ...????....)
>
> and process it in factories section(getServiceConfig) or in some other one?
--
Matthew Weier O'Phinney
Project Lead | [email protected]
Zend Framework | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc
--
List: [email protected]
Info: http://framework.zend.com/archives
Unsubscribe: [email protected]