简体   繁体   中英

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 :

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

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';

I am not very experienced with zend so I dont know where to start with. 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. That includes local.php, global.php from config/autoload and all used modules' 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 . There are preserved keys for some ZF2-specific configs, service_manager, etc.

$serviceManager->get('config');

There is a "standard" service pattern in 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.

Further, you have to register your own service/factory in the factories section of service_manager config in your module's module.config.php:

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 . 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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