在zf2模块中处理404错误页面

时间:2015-08-24 14:22:07

标签: php templates module zend-framework2

我有两个模块:

->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错误页面。

2 个答案:

答案 0 :(得分:0)

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

'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',

答案 1 :(得分:0)

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

您可以通过执行以下操作来查看合并的配置结果:

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

layout/doctorlayout/patient是唯一名称,因此不会被覆盖。但是密钥error/404并不是唯一的,因此它将被覆盖。

您可能会在Doctor模块之后加载Patient模块,以便显示医生模块中的error/404

如果按相反的顺序加载,它会显示患者模块中的error/404(试试:) :)。

因此,所有这些合并的配置意味着您只能在not_found_template密钥下定义exception_templateview_manager一次:

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

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