Zf3控制器无法访问位于另一个模块中的模型类表 [英] Zf3 controller not able to access the model class table located in another module

查看:88
本文介绍了Zf3控制器无法访问位于另一个模块中的模型类表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Zend Framework的新手. 有没有办法从我的活动控制器访问位于另一个模块中的模型类表?作为ZF3中的再见服务定位器,我无法访问位于其他模块中的模型类表.

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.

以前在ZF2控制器中

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();
    ......

}

由于服务定位器足以将函数从控制器调用到位于另一个模块中的模型类. 没有服务定位器,有没有办法在ZF3中实现类似的功能?

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?

先谢谢大家. 再见!

推荐答案

它在ZF3中的再见服务定位器

its bye bye service locator in ZF3

服务定位器尚未从ZF3中删除.但是,新版本的框架引入了一些更改,如果您依靠ServiceLocatorAwareInterface和/或将服务管理器注入到控制器/服务中,则会破坏现有代码 .

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.

在ZF2中,默认操作控制器实现了此接口,并允许开发人员从控制器内部获取服务管理器,就像您的示例一样.您可以在迁移指南中找到有关更改的更多信息. a>.

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.

首先,更新控制器.

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,
],

这篇关于Zf3控制器无法访问位于另一个模块中的模型类表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆