In controller I create and use my model so
public function getAlbumTable()
{
if (!$this->albumTable) {
$sm = $this->getServiceLocator();
$this->albumTable = $sm->get('Album\Model\AlbumTable');
}
return $this->albumTable;
}
How do I use this global Service Locator in another place of my project, for example, in the other model, and not only in any controller?
Сonfiguration of the connection to the database is defined in the file: my_project/config/autoload/global.php
Thank you.
getServicelocator()
makes error. So it needs alternative way. And extendsAbstractTableGateway
orServiceLocatorAwareInterface
have errors.Factory implementation will help Controller to get objects.
*User sample code will be similar to album.
1) factory class ( RegisterControllerFactory.php) * copied function createUser in controller
2) controller( RegisterController ) namespace Users\Controller;
3) module.config.php
Decided. So. For solving the task of classes of models must implement the interface ServiceLocatorAwareInterface. So injection ServiceManager will happen in your model automatically. See the previous example.
For forms and other objects of your application suitable method proposed here http://michaelgallego.fr/blog/?p=205 You can to create a base class form extends BaseForm and implements ServiceManagerAwareInterface, from which you will inherit its forms in the application.
To injection of the object of the ServiceManager was automatically in the file module.config.php in section service_manager you need to write
Then in your controller, you can create a form so
The form will contain an object ServiceManager, which will allow other dependencies.
Thanks all for your help.
Zend MVC will inject the ServiceLocator instance into a class implementing Zend\ServiceManager\ServiceLocatorAwareInterface. A simple implementation for a model table looks like the following: