5

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

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

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

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

但我不明白怎么做。我看不到请求到达'error/404'.

  • 如何ViewModel为它创造新的?

  • 如何将变量附加到它?

  • 路线是怎么'error/404'来抓它的?

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

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'. 请帮忙。

更新 1

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

'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'模板?

4

7 回答 7

5

还有另一种方法。赶上EVENT_DISPATCH_ERROR并完全重建viewModel。Cavern 是那个布局 - 是一个 root viewModel,默认情况下附加到布局中的内容是另一个viewModel(子)。这些点在官方文档中描述得不是很清楚。

这是它在您的 中的样子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' );
    });
}
于 2013-11-06T13:41:26.840 回答
3

我用它来管理 404 错误(我将我的网站从 spip 移动到基于 ZF2 的 cms):

在模块 onBootstrap 函数中:

$eventManager->getSharedManager()->attach('*', MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'onDispatchError'), -100);

然后

public function onDispatchError(MvcEvent $event)
{
    $response = $event->getResponse();
    if ($response->getStatusCode() == 404) {
        $url = $event->getRouter()->assemble(array(), array('name' => 'index'));
        $requestUri = $event->getRequest()->getRequestUri();
        $response->getHeaders()->addHeaderLine('Location', "$url?url=$requestUri");
        $response->setStatusCode(200);
        $response->sendHeaders();
        $event->stopPropagation(true);
    } elseif($response->getStatusCode() == 500){
        //DO SOMETHING else?
        return;
     }
}

在这段代码中,我们从不返回 404 错误,我们只是使用请求的 url 作为参数调用路由(在我的示例索引中)

我希望对你有所帮助。

于 2013-08-15T11:21:59.113 回答
2

首先是创建视图:

模块/您的应用程序/视图/您的应用程序/错误/index.phtml

模块/Yourapplication/view/yourapplication/error/404.phtml

第二件事是在模块配置中注册视图:

在您的应用程序的 module.config.php 中

'view_manager' => array(
        //[...]
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => array(
        //[...]
        'error/404'   => __DIR__ . '/../view/yourapplication/error/404.phtml',
        'error/index' => __DIR__ . '/../view/yourapplication/error/index.phtml',
        ),
   // [...]
    ),
于 2013-07-09T12:33:33.387 回答
2

我不确定我是否遵循您要实现的目标,您能否举一个清楚的例子说明您要做什么?

如果您只是尝试向传递的视图模型添加变量,您甚至可以在控制器中执行此操作,请查看AbstractActionController

/**
 * Action called if matched action does not exist
 *
 * @return array
 */
public function notFoundAction()
{
    $response   = $this->response;
    $event      = $this->getEvent();
    $routeMatch = $event->getRouteMatch();
    $routeMatch->setParam('action', 'not-found');

    if ($response instanceof HttpResponse) {
        return $this->createHttpNotFoundModel($response);
    }
    return $this->createConsoleNotFoundModel($response);
}

/**
 * Create an HTTP view model representing a "not found" page
 *
 * @param  HttpResponse $response
 * @return ViewModel
 */
protected function createHttpNotFoundModel(HttpResponse $response)
{
    $response->setStatusCode(404);

    // Add in extra stuff from your ServiceLocator here...

    // $viewModel->setTemplate(..); 

    return new ViewModel(array(
        'content' => 'Page not found',
    ));
}
于 2013-01-16T15:18:15.263 回答
2

module.php您还可以更改模板和布局。

function onBootstrap(EventInterface $e) {
  $app = $e->getApplication();
  $evt = $app->getEventManager();
  $evt->attach(MvcEvent::EVENT_DISPATCH_ERROR, array($this,'onDispatchError'), 100);    
}

function onDispatchError(MvcEvent $e) {
  $vm = $e->getViewModel();
  $vm->setTemplate('layout/blank');
}
于 2015-04-25T06:42:52.763 回答
2

或者更简单地说(在你想要的地方):

/* @var \Zend\Mvc\View\Http\RouteNotFoundStrategy $strategy */
$strategy = $this->getServiceLocator()->get('HttpRouteNotFoundStrategy');
$strategy->setNotFoundTemplate('application/other/404');

$view = new ViewModel();
//$view->setTemplate('application/other/404');
return $view;
于 2016-06-24T08:13:44.567 回答
0

您应该首先在 Module.php 中分离默认的 notfoundstrategy,如果出现 404,您应该从控制器返回一个新的视图模型。请看这篇文章: http: //www.cagataygurturk.com/zf2-controller-specific-not-found-page/

于 2015-04-17T12:39:13.990 回答