2

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!

khukuri
  • 93
  • 1
  • 7
  • 1. You can use `DI` in constructor in you controller. 2. Why your controller know about table from another module ? – newage Aug 01 '16 at 12:27
  • @newage thank you for the suggestion and i did use DI. I was trying to access the already created function in another module's model to avoid redundancy. – khukuri Aug 02 '16 at 05:58

1 Answers1

7

its bye bye service locator in ZF3

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 ServiceLocatorAwareInterface and/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.

namespace Foo\Controller;

use Config\Model\ConfigTable; // assuming this is an actual class name

class FooController extends AbstractActionController
{
    private $configTable;

    public function __construct(ConfigTable $configTable)
    {
        $this->configTable = $configTable;
    }

    public function indexAction()
    {
        $config = $this->configTable->getAllConfiguration();
    }

    // ...
}

Then create a new service factory that will inject the config table dependency into the controller (using the new ZF3 factory interface)

namespace Foo\Controller;

use Foo\Controller\FooController;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\FactoryInterface;

class FooControllerFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        $configTable = $container->get('Config\Model\ConfigTable');

        return new FooController($configTable);
    }
}

Then update the configuration to use the new factory.

use Foo\Controller\FooControllerFactory;

'factories' => [
    'Foo\\Controller\\Foo' => FooControllerFactory::class,
],
AlexP
  • 9,906
  • 1
  • 24
  • 43
  • Thanks a lot!!!! you are awesome @AlexP . Service Manager Migration document helped me lot and the example was really great and easy to understand. – khukuri Aug 02 '16 at 05:52
  • @PrashantKasajoo Should we write createService() methode? In older version Service locator used as a parameter. What should i write inside that function? I am newbie to zend... – CJ Ramki Aug 04 '16 at 12:49
  • @PrashantKasajoo I have got this error `Class Application\Factory\IndexFactory contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Zend\ServiceManager\FactoryInterface::createService)` – CJ Ramki Aug 04 '16 at 13:12
  • @CJRamki The answer shows how to create a factory specifically for ZF3. The error is because you are using *ZF2* and the `Zend\ServiceManager\FactoryInterface` is one of the interfaces that was updated. You will need to use the `createService()` method or update to ZF3 for the above example to work. – AlexP Aug 04 '16 at 16:37
  • @AlexP How to check my current vendor directory zend libraries are ZF3 or ZF2? Because each components having their own versions. – CJ Ramki Aug 05 '16 at 05:06
  • @AlexP I am getting this "This table does not have an Adapter setup". Can you please tell reason for this message? can pls point out where i missed? – CJ Ramki Aug 05 '16 at 07:35
  • @CJRamki if you have upgraded to Zf3 using composer then your libraries should also be updated according to it. please go through the miragtion documentation of zf3 and you will find the difference between them and you will catch up with zf3 within a day or two. – khukuri Aug 05 '16 at 09:33
  • @CJRamki there are lot of reason error may be causing. please take a look zf2 manual : http://zf2.readthedocs.org/en/latest/user-guide/database-and-models.html – khukuri Aug 05 '16 at 09:38