本文介绍了ZF2-如何更改错误/404响应页面?不仅是模板,还可以设置一个新的ViewModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,页面在Application module.config数组中设置如下:

By default the page is set like this in the Application module.config array:

'template_map' => array(
    'error/404' => __DIR__ . '/../view/error/404.phtml'

我要更改页面.我想要新的ViewModel充满变量.这意味着仅更改模板是不够的:

I want to change the page. I want new ViewModel for it full of variables. It means that simply to change the template is not enough:

'error/404' => __DIR__ . '/../view/error/my_new_404_template.phtml'

但是我不知道该怎么做.我看不到请求到达'error/404'的方式.

But I can't understand how to make it. I can't see the way the request comes to the 'error/404'.

  • 如何为其创建新的ViewModel?

如何附加变量?

如何到达'error/404'的路线以赶上它的变化?

How the route comes to 'error/404' to catch it to change?

例如,对于'error/404'页面,我有这样一种方法:

For example, I have such a method for 'error/404' page:

public function pageNotFoundAction() {

    $view = new ViewModel();

    $view->setTemplate('error/404');     // set my template

    $sm = $this->getServiceLocator()->get('SessionManager');
    $cont = new Container('SomeNamespace', $sm);

    $view->var1 = $cont->offsetGet('someValue1');   // the "error/404" template
    $view->var2 = $cont->offsetGet('someValue2');   //       is full of variables
    $view->var3 = $cont->offsetGet('someValue3');
    $view->var4 = "One more view variable";

    // And now I return it somewhere and want it to be called
    //    in case of "the page is not found"
    return $view;
}

如何进行此类更改?我无法获得他们创建的用于处理诸如'error/404'之类的系统的系统.请帮忙.

How to make such a change? I can't get the system they've created to deal with things like 'error/404'. Please help.

UPD 1

更复杂的任务.如何有几个'error/404'页?想问问那些创建框架的人是否知道'not_found_template'不能有一个'error/404'页面数组.怎么可能呢?如果我这样设置此选项:

Even more complicated task. How to have several 'error/404' pages?Would like to ask guys who created the framework whether they know that 'not_found_template' cannot have an array of 'error/404' pages. How it can be? If I set this option like this:

'template_map' => array(
    'error/404' => __DIR__ . '/../view/error/404.phtml',
    'my-page/one_more_error404' => __DIR__ . '/../view/my-page/my-page/one_more_error404.phtml',
    'other_page/second_404' => __DIR__ . '/../view/other-page/one_more_error404.phtml',

'not_found_template' => array(
    'error/404',
    'my-page/one_more_error404',
    'other-page/second_404',
);

它抛出一个错误. 'not_found_template'强制您只有一个'error/404'模板吗?

it throws an error. 'not_found_template' force you to have only one 'error/404' template?

推荐答案

还有另一种方法.捕获EVENT_DISPATCH_ERROR并完全重建viewModel. Cavern是布局-是根viewModel,默认情况下附加到布局的内容是另一个viewModel(子级).这些要点在官方文档中没有这么清楚地描述.

There is another way. To catch an EVENT_DISPATCH_ERROR and totally rebuild viewModel. Cavern is that layout – is a root viewModel, and content appended into layout by default is another viewModel (child). These points are not such clear described in official docs.

这是您的Module.php中的样子:

public function onBootstrap(MvcEvent $event)
{
    $app = $event->getParam( 'application' );
    $eventManager = $app->getEventManager();


    /** attach Front layout for 404 errors */
    $eventManager->attach( MvcEvent::EVENT_DISPATCH_ERROR, function( MvcEvent $event ){

        /** here you can retrieve anything from your serviceManager */
        $serviceManager = $event->getApplication()->getServiceManager();
        $someVar = $serviceManager->get( 'Some\Factory' )->getSomeValue();

        /** here you redefine layout used to publish an error */
        $layout = $serviceManager->get( 'viewManager' )->getViewModel();
        $layout->setTemplate( 'layout/start' );

        /** here you redefine template used to the error exactly and pass custom variable into ViewModel */
        $viewModel = $event->getViewModel();
        $viewModel->setVariables( array( 'someVar' => $someVar ) )
                  ->setTemplate( 'error/404' );
    });
}

这篇关于ZF2-如何更改错误/404响应页面?不仅是模板,还可以设置一个新的ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 14:39