【问题标题】:Zend Framework Config file JSONZend 框架配置文件 JSON
【发布时间】:2013-02-04 07:47:36
【问题描述】:

目前我正在学习 ZF2。在通过“入门”时,我看到模块的每个配置文件都充满了 PHP 数组。文档中的一个示例:

<?php
return array(
    'controllers' => array(
        'invokables' => array(
            'Album\Controller\Album' => 'Album\Controller\AlbumController',
        ),
    ),

    // The following section is new and should be added to your file
    'router' => array(
        'routes' => array(
            'album' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/album[/:action][/:id]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9]+',
                    ),
                    'defaults' => array(
                        'controller' => 'Album\Controller\Album',
                        'action'     => 'index',
                    ),
                ),
            ),
        ),
    ),

    'view_manager' => array(
        'template_path_stack' => array(
            'album' => __DIR__ . '/../view',
        ),
    ),
);

数组,其中包含数组。实际上我知道,该数组只是函数的名称,它更像是带有键/值对的映射。

Zend MODS 之一指出我们可以将 JSON 用于配置文件: http://framework.zend.com/manual/2.0/en/user-guide/routing-and-controllers.html#comment-696979913

有人可以为初学者提供示例吗?我真的更喜欢对这些文件配置使用 JSON 格式而不是数组/映射,但我在 ZF 主页上找不到它。或者我不应该这样做?

【问题讨论】:

  • 我建议不要使用 JSON。一段时间后,您会想在配置文件中为 ServiceManager 创建一个单行工厂,然后您将需要在 php 中使用它。
  • 如果所有的array( 都让你觉得很烦并且让它看起来很丑,那么使用php 5.4 的[] 数组语法。

标签: php json zend-framework2


【解决方案1】:

我会尝试修改 Module.php 文件中的 getConfig() 函数:

return \Zend\Config\Factory::fromFile(__DIR__ . '/config/module.config.json', false);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    相关资源
    最近更新 更多