php - 在 ZF3 中获取模块配置

标签 php model-view-controller configuration module zend-framework3

我想从 Zend Framework 3 中的 Controller 检索我的模块配置。 我搜索了一下,似乎在 ZF2 中执行此操作的标准方法是使用

$this->getServiceLocator()

访问 module.config.php 中的配置。 但是,这在 ZF3 中不起作用,因为没有 getServiceLocator() 方法。

实现这一目标的标准方法是什么?

最佳答案

不知道您是否找到了答案,因为正如 tasmaniski 所写,有不同的解决方案。为了以防万一,让我分享一个在我开始玩 ZF3 时对我有很大帮助的:

MyControllerFactory.php

<?php
namespace My\Namespace;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use DependencyNamespace\...\ControllerDependencyClass; // this is not a real one of course!

class MyControllerFactory implements FactoryInterface
{
    /**
     * @param ContainerInterface $container
     * @param string $requestedName
     * @param null|array $options
     * @return AuthAdapter
     */
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        // Get config.
        $config = $container->get('configuration');

        // Get what I'm interested in config.
        $myStuff = $config['the-array-i-am-interested-in']

        // Do something with it.
        $controllerDepency = dummyFunction($myStuff);

        /*...the rest of your code here... */

        // Inject dependency.
        return $controllerDepency;
    }
}

MyController.php

<?php
namespace My\Namespace;

use Zend\Mvc\Controller\AbstractActionController;
use DependencyNamespace\...\DependencyClass;

class MyController extends AbstractActionController
{
    private $controllerDepency;

    public function __construct(DependencyClass $controllerDepency)
    {
        $this->controllerDepency = $controllerDepency;
    }

    /*...the rest of your class here... */
}

关于php - 在 ZF3 中获取模块配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39163268/

相关文章:

model-view-controller - 如何将逻辑排除在 cakephp View 之外?

android - 我如何在 android 中的方向更改期间查看方法/单步执行代码

asp.net-core - 手动触发IOptionsMonitor<>.OnChange

maven - Maven 配置文件中 "properties-maven-plugin"和 "filter"的用途是什么

php - 如何在 PHP 中查询数组并为每个元素返回单独的行/项目

php - 嵌套事务在 Laravel 中是如何工作的?

php - codeigniter 将字段名称和行值放入单个字符串中

java - 如何将此 JSP 转换为 MVC 方法?

php - 具有父主题和子主题的网站的 MySQL 结构

java - spring mvc InternalResourceViewResolver 没有得到前缀