简体   繁体   中英

Get configuration Zend Framework 2

I have a project with Zend Framework and i trying to rewrite this peoject on Zend Framework 2. In old project i have some environment dependent settings in application.ini

[Prod]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"

resources.frontController.params.displayExceptions = 1

theme = Test

copyright = TestText
resources.db.adapter = pdo_mysql
resources.db.params.host = 127.0.0.1
resources.db.params.username = root
resources.db.params.password = adm$123
resources.db.params.dbname = test;

I controller some value from aplication.ini has retrived.

   $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
    $aConfig = $bootstrap->getOptions();
    $this->view->assign('theme', $aConfig['theme']);
    $this->view->assign('copyright', $aConfig['copyright']);

I download skeleton-application with Zend Framework 2 , add new module. But how I can do similar in my new project? Where and how I should describe settings from old project? How I can retrieve it?

You can easily use the AbstractOptions class for your ZF2 module. Let 's assume your ZF2 module is called application. So it 's stored in /module/Application/ folder.

First you need the ModuleOptions class under /module/Application/src/Options/. In this class you can write down all your settings you need for your module. For example reasons I only write the copyright member in the class.

declare('strict_types=1');
namespace Application\Options;
use Zend\StdLib\AbstractOptions;

class ModuleOptions extends AbstractOptions
{
    protected $copyright = 'my copyright';

    public function getCopyright() : string
    {
        return $this->copyright;
    }

    public function setCopyright(string $copyright) : ModuleOptions
    {
        $this->copyright = $copyright;
        return $this;
    }
}

Further you need a factory for your module options class. This factory could look like the following example.

declare('strict_types=1');
namespace Application\options\Factory;

use Application\Options\ModuleOptions;
use Interop\Container\ContainerInterface;

class ModuleOptionsFactory
{
    public function __invoke(ContainerInterface $container) : ModuleOptions
    {
        $config = $container->get('config');
        $options = new ModuleOptions(isset($config['my_settings']) ? $config['my_settings'] : []);
        return $options;
    }
}

Basicly that 's all you need. Just wrap it up in your module.config.php like the following example.

...
'service_manager' => [
    'factories' => [
        ModuleOptions::class => ModuleOptionsFactory::class,
    ]
],
'my_settings' = [
    'copyright' => 'another copyright text',
]

The ModuleOptions class takes the my_settings array from your module.config.php and makes it accessable wherever a service locator is.

Example for use in a controller

For example you could use the ModuleOptions class in a controller factory like in the following example.

class IndexControllerFactory
{
    public function __invoke(ContainerInterface $container)
    {
        $container = $container->getServiceLocator();
        $options = $container->get(ModuleOptions::class);

        return new IndexController($options);
    }
}

Your IndexController class looks something like this. In this example we avoid calling the service locator in the controller itself because this is a bad practice. We just pass the options as a argument in the constructor.

class IndexController extends AbstractActionController
{
    protected $options;

    public function __construct(ModuleOptions $options)
    {
        $this->options = $options;
    }

    public function indexAction()
    {
        return [
            'copyright' => $this->options->getCopyright(),
        ];
    }

Enjoy! ;)

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