【问题标题】:handing the 404 error page in zf2 modules在 zf2 模块中处理 404 错误页面
【发布时间】:2015-08-24 14:22:06
【问题描述】:

我有两个模块:

->Patient
->Doctor

project/ 指向Patient 模块,project/doctor 指向Doctor 模块。

所以当我输入project/forgotpass 时,它应该指向来自Patient 模块的404 错误页面,但它指向我的是Doctor 404 错误页面。

我怎样才能设法指向相应的 404 错误页面。

在Patient 模块配置中:

'view_manager' => array(
    'display_not_found_reason' => true,
    'display_exceptions'       => true,
    'doctype'                  => 'HTML5',
    'not_found_template'       => 'error/404',
    'exception_template'       => 'error/index',
    'template_map' => array(
        'layout/patient' => __DIR__ . '/../view/layout/layout.phtml',
        'error/404'      => __DIR__ . '/../view/error/404.phtml',
        'error/index'    => __DIR__ . '/../view/error/index.phtml',
    ),
    'template_path_stack' => array(
        __DIR__ . '/../view',
    )
)

在Doctor 模块配置中:

'view_manager' => array(
    'display_not_found_reason' => true,
    'display_exceptions'       => true,
    'doctype'                  => 'HTML5',
    'not_found_template'       => 'error/404',
    'exception_template'       => 'error/index',
    'template_map' => array(
        'layout/doctor' => __DIR__ . '/../view/layout/layout.phtml',
        'error/404'     => __DIR__ . '/../view/error/404.phtml',
        'error/index'   => __DIR__ . '/../view/error/index.phtml',
    ),
    'template_path_stack' => array(
        __DIR__ . '/../view',
    )
)

是否可以有单独的 404 页面,或者我应该调用相应的模块 404 错误页面。我的问题是Patient 模块正在调用Doctor 404 错误页面。

【问题讨论】:

    标签: php templates module zend-framework2


    【解决方案1】:

    将模板名称更改为不同的:

    'not_found_template'       => 'error/patient/404',
    'exception_template'       => 'error/patient/index',
    

    并在模板映射中使用它们:

    'error/patient/404'               => __DIR__ . '/../view/error/404.phtml',
    'error/patient/index'             => __DIR__ . '/../view/error/index.phtml',
    

    【讨论】:

    • 这是你的全局配置还是模块配置?
    【解决方案2】:

    来自不同模块的配置数组在引导程序中全部合并到一个应用程序范围的配置数组中。这意味着具有相同名称的键将被最后加载的文件覆盖。

    您可以通过以下方式查看合并后的配置结果:

    $config = $serviceManager->get('Config');
    

    layout/doctor 和 layout/patient 是唯一名称,因此不会被覆盖。但是error/404 的键不是唯一的,所以会被覆盖。

    您可能会在Patient 模块之后加载Doctor 模块,因此您的医生模块中的error/404 将显示出来。

    如果您以相反的顺序加载,它将显示来自 Patient 模块的error/404(试试看:))。


    因此,所有这些配置合并意味着您只能在 view_manager 键下定义 not_found_template 和 exception_template 一次:

    'view_manager' => array(
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index'
    )
    

    错误页面通常在 Application 模块中定义一次,并用于整个 ZF2 应用程序中的所有模块。

    【讨论】:

    • @user3929758 你看我的回答了吗?您现在了解如何在配置中设置模板了吗?
    猜你喜欢
    • 1970-01-01
    • 2013-09-19
    • 2014-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多