【问题标题】:Using a Zend Framework 2 Layout. Default Layout使用 Zend Framework 2 布局。默认布局
【发布时间】:2014-04-06 00:49:55
【问题描述】:

我从一个配置的模块 Common 开始:

<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

return array(
    'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                        'controller' => 'Common\Controller\Index',
                        'action'     => 'index',
                    ),
                ),
            ),
        ),
    ),
    'service_manager' => array(
        'factories' => array(
            'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
        ),
    ),
    'translator' => array(
        'locale' => 'en_US',
        'translation_file_patterns' => array(
            array(
                'type'     => 'gettext',
                'base_dir' => __DIR__ . '/../language',
                'pattern'  => '%s.mo',
            ),
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'Common\Controller\Index' => 'Common\Controller\IndexController'
        ),
    ),
    'view_helpers' => array(
        'invokables' => array(
            'flashMessengerHelper' => 'Common\View\Helper\FlashMessenger',
        ),
    ),
    'view_manager' => array(
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    ),
);

该模块包含: zf2-client\module\Common\view\layout\layout.phtml

当我访问路线时,我会看到一个页面并加载布局。我有另一个模块 Wall 并且已配置:

    <?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

return array(
    'router' => array(
        'routes' => array(
            'wall' => array(
                'type' => 'Zend\Mvc\Router\Http\Segment',
                'options' => array(
                    'route'    => '/:username',
                    'constraints' => array(
                        'username' => '\w+'
                    ),
                    'defaults' => array(
                        'controller' => 'Wall\Controller\Index',
                        'action'     => 'index',
                    ),
                ),
            )
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'Wall\Controller\Index' => 'Wall\Controller\IndexController'
        ),
    ),
    'view_manager' => array(
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    ),
);

当我访问路由'/:username'时,我注意到Common 模块中描述的布局正在自动加载。 Wall 模块如何知道从 common 加载布局? Wall 模块在其视图文件夹中不包含布局,但似乎是从 Common 加载的。如果 Zend 的网站上有任何文档描述了布局系统的此功能,我还无法挖掘出来。 Wall 或 Common 的控制器中没有任何内容表明正在设置模板。所以任何地方的控制器代码中都没有 setTemplate。

感谢您发布...

【问题讨论】:

    标签: layout zend-framework2


    【解决方案1】:

    您发布的配置正在向视图管理器的模板路径堆栈添加两个路径:zf2-client\module\Common\viewzf2-client\module\Wall\view。默认情况下,ZF2 会查找名为 layout/layout 的视图进行渲染。它将检查视图堆栈以查找匹配的文件。由于您只有其中一个,并且它在您的 Common 模块中,所以这就是使用的。

    【讨论】:

      猜你喜欢
      • 2012-08-22
      • 2012-12-15
      • 2012-10-22
      • 1970-01-01
      • 2015-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多