ZF2 - How to change the error/404 response page? Not just template but to set a new ViewModel

前端 未结 7 2527
离开以前
离开以前 2021-01-05 22:46

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

\'template_map\' => array(
    \'error/404\' => __DIR__ . \'/         


        
7条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-05 23:39

    I am not sure I follow what you are trying to achieve, can you give a clear example of what you are trying to do?

    If you are simply trying to add variables to the view model passed you could even do this in your controller, check out 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',
        ));
    }
    

提交回复
热议问题