简体   繁体   中英

handing the 404 error page in zf2 modules

I have two modules:

->Patient
->Doctor

project/ points to Patient module and project/doctor points to the Doctor module.

So when I type the project/forgotpass , it should point to the 404 error page from the Patient module, but it points me to the Doctor 404 error page.

How can I manage to point to the respective 404 error pages.

In the Patient module config:

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

And in the Doctor module config:

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

Is it okay to have separate 404 pages, or should I call respective module 404 error page. My issue here is that the Patient module is calling the Doctor 404 error page.

Change the name of the templates to different ones:

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

And use them in the template map:

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

The config arrays from the different modules are on bootstrap all merged into one application wide config array. This means that keys with the same name will be overwritten by the last loaded file.

You can see the merged config results by doing:

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

The layout/doctor and layout/patient are unique names and thus they will not be overwritten. But the key error/404 is not unique so it will be overwritten.

You probably load the Doctor module after the Patient module so the error/404 from your doctor module will be shown.

If you load in the reverse order it will show you the error/404 from your Patient module (try it :) ).


So all this merging of configs means you can define the not_found_template and exception_template under the view_manager key only once:

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

Error pages are usually defined once in a Application module and used for all modules throughout the whole ZF2 application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM