简体   繁体   English

使用zend全局配置的许多模块中使用的ZF2模型

[英]ZF2 Model used in many modules using zend global config

I have a Class object which is used in many modules in my zend structure : 我在zend结构的许多模块中都有一个Class对象:

/module/
--|Aplication
--|MyClassModule
----|config
----|src
------|Factory
------|Model
---------|> MyObjectClass.php 
----Module.php
--|AnotherModule

So my idea is to use this MyObjectClass.php in other modules so I can avoid duplication and have its own configuration. 因此,我的想法是在其他模块中使用此MyObjectClass.php,以便避免重复并拥有自己的配置。 So far, for this is ok, however I want to get the variables set from my config/autoload files injected in this class but I don't know how. 到目前为止,这是可以的,但是我想从此类中注入的config / autoload文件中获取变量集,但我不知道如何。

How can I load this config data into my class model? 如何将该配置数据加载到类模型中? Which is the best approach ? 哪种方法最好? I can load it by accessing this directly but I don't think this is very elegant 我可以通过直接访问它来加载它,但我认为这不是很优雅

eg: $configArray = require './config/autoload/config.local.php'; 例如: $configArray = require './config/autoload/config.local.php';

I am not very experienced with zend so I dont know where to start with. 我对zend不太了解,所以我不知道从哪里开始。 I have seen many tutorials of how to do this via controllers, views.. etc but not in specific classes. 我已经看过许多关于如何通过控制器,视图等等实现此目的的教程,但是在特定的类中却没有。

Thank you. 谢谢。

All config files are merged into one config, when your ZF2 application is bootstrapped. 引导ZF2应用程序时,所有配置文件都将合并为一个配置。 That includes local.php, global.php from config/autoload and all used modules' module.config.php. 其中包括config/autoload local.php,global.php以及所有使用的模块的module.config.php。 With a bit of more research, you can overwrite the standard loading, eg loading custom configs. 经过更多研究,您可以覆盖标准加载,例如,加载自定义配置。

After bootstrapping, your are able to access the config from the ServiceManager . 引导后,您可以从ServiceManager访问配置。 There are preserved keys for some ZF2-specific configs, service_manager, etc. 有些ZF2特定的配置,service_manager等都有保留的密钥。

$serviceManager->get('config');

There is a "standard" service pattern in ZF2: Factory . ZF2中有一个“标准”服务模式: Factory This can be applied for Controllers, Services. 这可以应用于控制器,服务。 What ever you want. 无论你想要什么。

namespace Application\Factory;

use Application\Model\MyObjectClass;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class MyObjectFactory implements FactoryInterface
{
    /**
     * Create service
     *
     * @param ServiceLocatorInterface $serviceLocator
     * @return mixed
     */
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        // get some config parameter, inject it into model
        $config = $serviceLocator->get('config');

        $myObjectClass = new MyObjectClass();
        // ... e.g. $myObjectClass->setConfig($config);
        return $myObjectClass;
    }
}

It should be clear, what this factory is made for: create and return an instance of your custom object ;) You may configure your instance with some config params. 应该清楚,该工厂的用途是:创建并返回自定义对象的实例;)您可以使用一些配置参数来配置实例。 With ServiceLocator as method param, you are able to access the config, other services etc. 使用ServiceLocator作为方法参数,您可以访问配置,其他服务等。

Further, you have to register your own service/factory in the factories section of service_manager config in your module's module.config.php: 此外,您必须在模块的module.config.php中的service_manager config的factories部分中注册自己的服务/工厂:

return array(
    'service_manager' => array(
        'factories' => array(
            'MyObjectFactory' => 'Application\Factory\MyObjectFactory',
        ),
    ),
);

Now you should be able to access your factory, eg in an ActionController or wherever you have access to ServiceManager . 现在,您应该可以访问工厂,例如在ActionController或可以访问ServiceManager任何地方。 That means, you can also access this factory from different modules. 这意味着,您还可以从其他模块访问该工厂。

public function someCustomAction() {
    $myObjectClass = $this->getServiceLocator()->get('MyObjectFactory');
    $myObjectClass2 = $this->getServiceLocator()->get('MyObjectFactory');

    var_dump($myObjectClass);
    var_dump($myObjectClass2);

    if ($myObjectClass === $myObjectClass2) {
        echo '<br />equal';
    }

    $myObjectClass = new MyObjectClass();
    $myObjectClass2 = new MyObjectClass();

    var_dump($myObjectClass);
    var_dump($myObjectClass2);
}

Note: Be aware, that ServiceManager returns the same instance of your object. 注意:请注意, ServiceManager返回对象的相同实例。 So, that seems like what you ask for? 所以,这似乎是您要的? In contrast, creating a new instance will create different objects. 相反,创建新实例将创建不同的对象。

Note 2: Tested with ZF2 v2.4.9 注意2:已测试与ZF2 v2.4.9

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

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