简体   繁体   English

Zend框架2访问模型类中的全局配置

[英]Zend framework 2 accessing global config in model class

I have a model class which does not extend any core Zend module . 我有一个模型类,它不扩展任何核心Zend模块。 This model was imported from my previous Zend framework 1 application . 此模型是从我之前的Zend框架1应用程序导入的。 I am able to call its methods by converting it to namespace . 我可以通过将其转换为命名空间来调用它的方法。 The problem what I have is in reading global configuration in side the methods defined . 我所遇到的问题是在定义的方法中读取全局配置。

In case of controller I was able to access global configuration using below code 在控制器的情况下,我能够使用下面的代码访问全局配置

 $config = $this->getServiceLocator()->get('config'); 

// This gives a union of global configuration along with module configuration .

But what should we do to access configuration in side a model class . 但是我们应该如何在模型类中访问配置呢。 Below is how my model class is 以下是我的模型类的方法

<?php
namespace test\Http; 

class Request
{

    protected $client;

    public function abc( $c)
    {
        return $something;
    } 


    ......

} 

I am new to Zend framework 2 please kindly suggest any method to achieve this . 我是Zend框架2的新手,请提出任何方法来实现这一目标。

In the above description model means ( MVC model class ) which has some business logic in it . 在上面的描述中,模型意味着(MVC模型类),其中包含一些业务逻辑。

Assuming that you build your service (your code looks like a service) you will probably instantiate it in a service factory (in this case I've put it in the module config): 假设您构建服务(您的代码看起来像服务),您可能会在服务工厂中实例化它(在这种情况下,我将它放在模块配置中):

class MyModule
{
    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'my_request_object' => function (
                    \Zend\ServiceManager\ServiceLocatorInterface $sl
                ) {
                    $config = $sl->get('config'); 

                    return new \GaGooGl\Http\Request($config);
                },
            ),
        );
    }
}

This way, you are injecting the config object directly in its consumer (without having a reference to the service locator in the consumer) 这样,您直接在其使用者中注入配置对象(没有对使用者中的服务定位器的引用)

Another way is to implement Zend\\ServiceManager\\ServiceLocatorAwareInterface in your GaGooGl\\Http\\Request . 另一种方法是在GaGooGl\\Http\\Request实现Zend\\ServiceManager\\ServiceLocatorAwareInterface I personally discourage it , but this basically allows you to have your Request object keep a reference to the service locator internally, therefore making it possible to retrieve the config service at runtime. 个人不鼓励它 ,但这基本上允许你让你的Request对象在内部保持对服务定位器的引用,因此可以在运行时检索config服务。

最简单的方法

$config = new \Zend\Config\Config( include APPLICATION_PATH.'/config/autoload/global.php' ); 

Check this . 检查一下 It has two solutions. 它有两个解决方案。 One is implementing a Service locator aware interface. 一个是实现服务定位器感知接口。 Another is to inject the service manager into your model. 另一种方法是将服务管理器注入您的模型。 For both, you need to instantiate your model object through the service manager. 对于这两者,您需要通过服务管理器实例化模型对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM