如何发送到Zend Framework的自定义错误页面? [英] How to send to custom error pages Zend Framework?

查看:96
本文介绍了如何发送到Zend Framework的自定义错误页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有成员可以被阻止,当成员被阻止时,我想将它们发送到自定义错误页面,我如何在zend框架中这样做?我试过

  throw new Zend_Controller_Dispatcher_Exception('你的消息在这里'); 

但它不会在这里显示您的消息,当我发现页面未找到



这里是我的错误控制器。

  ?php 

class ErrorController extends Zend_Controller_Action
{

public function errorAction()
{
$ errors = $ this-> _getParam ( 'error_handler');

switch($ errors-> type){
case Zend_Controller_Plugin_ErrorHandler :: EXCEPTION_NO_ROUTE:
case Zend_Controller_Plugin_ErrorHandler :: EXCEPTION_NO_CONTROLLER:
case Zend_Controller_Plugin_ErrorHandler :: EXCEPTION_NO_ACTION:

// 404错误 - 控制器或动作未找到
$ this-> getResponse() - > setHttpResponseCode(404);
$ this-> view-> message ='未找到页面';
break;

// 666错误 - 控制器或动作未找到
$ this-> getResponse() - > setHttpResponseCode(666);
$ this-> view-> message ='被阻止';
break;
默认值:
//应用程序错误
$ this-> getResponse() - > setHttpResponseCode(500);
$ this-> view-> message ='应用程序错误';
break;
}

//如果记录器可用
if($ log = $ this-> getLog()){
$ log-> crit ($ this-> view-> message,$ errors-> exception);
}

//有条件地显示异常
if($ this-> getInvokeArg('displayExceptions')== true){
$ this-> view - > exception = $ errors-> exception;
}

$ this-> view-> request = $ errors-> request;
}

public function getLog()
{
$ bootstrap = $ this-> getInvokeArg('bootstrap');
if(!$ bootstrap-> hasPluginResource('Log')){
return false;
}
$ log = $ bootstrap-> getResource('Log');
return $ log;
}

}

这里是文档, http://framework.zend.com/manual/en/zend.exception.using .html 我不明白:(

解决方案

这里有一些关于使其工作的建议。 / p>

首先,您需要将错误与标准的 no_route / no_controller / no_action 错误区分开来,为此,您可以抛出你自己的自定义异常(你可以从PHP的异常类扩展它。

  throw new My_Exception_Blocked('你被阻止'); 

然后,修改错误控制器来查看像这样:

  public function errorAction()
{
$ errors = $ this-> _getParam ('error_handler');

switch($ errors-> type){
case Zend_Controller_Plugin_ErrorHandle r :: EXCEPTION_NO_ROUTE:
case Zend_Controller_Plugin_ErrorHandler :: EXCEPTION_NO_CONTROLLER:
case Zend_Controller_Plugin_ErrorHandler :: EXCEPTION_NO_ACTION:

// 404错误 - 控制器或操作未找到
$ this - > GETRESPONSE() - > setHttpResponseCode(404);
$ this-> view-> message ='未找到页面';
break;

//检查任何其他异常
case Zend_Controller_Plugin_ErrorHandler :: EXCEPTION_OTHER:
if($ errors-> exception instanceof My_Exception_Blocked){
$ this->的GetResponse() - > setHttpResponseCode(403);
$ this-> view-> message = $ errors-> exception-> getMessage();
break;
}
//如果不是类型My_Exception_Blocked

默认值:
//应用程序错误
$ this-> getResponse() - > ; setHttpResponseCode(500);
$ this-> view-> message ='应用程序错误';
break;
}
}

我还将响应代码从 666 403 ,因为没有 6xx HTTP错误代码,这可能会令人不安服务器和/或客户端。



希望有帮助。


I have members that can be blocked and when the member is blocked I want to send them to a custom error page, how would I do that in zend framework? I tried

throw new Zend_Controller_Dispatcher_Exception('Your message here');

but it doesn't say "your message here", it says "page not found" when I do this.

here is my error controller.

<?php

class ErrorController extends Zend_Controller_Action
{

public function errorAction()
{
    $errors = $this->_getParam('error_handler');

    switch ($errors->type) {
        case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
        case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
        case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:

            // 404 error -- controller or action not found
            $this->getResponse()->setHttpResponseCode(404);
            $this->view->message = 'Page not found';
            break;

            // 666 error -- controller or action not found
            $this->getResponse()->setHttpResponseCode(666);
            $this->view->message = 'Blocked';
            break;
        default:
            // application error
            $this->getResponse()->setHttpResponseCode(500);
            $this->view->message = 'Application error';
            break;
    }

    // Log exception, if logger available
    if ($log = $this->getLog()) {
        $log->crit($this->view->message, $errors->exception);
    }

    // conditionally display exceptions
    if ($this->getInvokeArg('displayExceptions') == true) {
        $this->view->exception = $errors->exception;
    }

    $this->view->request   = $errors->request;
}

public function getLog()
{
    $bootstrap = $this->getInvokeArg('bootstrap');
    if (!$bootstrap->hasPluginResource('Log')) {
        return false;
    }
    $log = $bootstrap->getResource('Log');
    return $log;
}

}

Here is the documentation, http://framework.zend.com/manual/en/zend.exception.using.html I don't understand it :(

解决方案

Here are a few suggestions on making it work.

First you need to differentiate your error from the standard no_route/no_controller/no_action errors. To do this, you can throw your own custom exception (you can extend this from PHP's Exception class.

throw new My_Exception_Blocked('You are blocked');

Then, modify the error controller to look like this:

public function errorAction()
{
    $errors = $this->_getParam('error_handler');

    switch ($errors->type) {
        case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
        case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
        case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:

            // 404 error -- controller or action not found
            $this->getResponse()->setHttpResponseCode(404);
            $this->view->message = 'Page not found';
            break;

        // check for any other exception
        case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER:
            if ($errors->exception instanceof My_Exception_Blocked) {
                $this->getResponse()->setHttpResponseCode(403);
                $this->view->message = $errors->exception->getMessage();
                break;
            }
            // fall through if not of type My_Exception_Blocked

        default:
            // application error
            $this->getResponse()->setHttpResponseCode(500);
            $this->view->message = 'Application error';
            break;
    }
}

I also changed the response code from 666 to 403 as there are no 6xx HTTP error codes and this could upset the server and/or client.

Hope that helps.

这篇关于如何发送到Zend Framework的自定义错误页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆