I am new to Zend Framework. Is there a way to access the model class table which is located in another module from my active controller? As its bye bye service locator in ZF3 i am not able to access the model class table located in other modules.
Previously in ZF2 controller
private configTable;
public function getConfigTable()
{
if (!$this->configTable) {
$sm = $this->getServiceLocator();
$this->configTable = $sm->get('Config\Model\ConfigTable'); // <-- HERE!
}
return $this->configTable;
}
public function indexAction(){
$allConfig = $this->getConfigTable()->getAllConfiguration();
......
}
As service locator was enough to call the function from controller to the model class located in another module. Is there a way to achieve something similar in ZF3 without service locator?
Thanks in advance guys. Bye!
The service locator has not been removed from ZF3. However, the new version of the framework has introduced some changes that will break existing code if you have relied on the
ServiceLocatorAwareInterfaceand/or having the service manager injected into your controllers/services.In ZF2 The default action controller implemented this interface and allowed developers to fetch the service manager from within the controller, just like in your example. You can find more information on the changes in the migration guide.
The recommended solution to this is to resolve all the dependencies of your controller within a service factory and inject them into the constructor.
Firstly, update the controller.
Then create a new service factory that will inject the config table dependency into the controller (using the new ZF3 factory interface)
Then update the configuration to use the new factory.