Zend Framework Application - Showing 404 Error Pages / Generic Error Pages in Production Mode and showing Exceptions in Development Mode
My goal is to achieve the following; in any environment, Production, Testing or Development, the application can have 2 modes: Production Mode and Debug Mode.
Currently, I have it setup so that the config.ini has this setting. No issues with that.
The problem comes when I want to achieve the following: If the Production Mode is set, I only want to show a custom error page, one with a 404 message if the Controller / Action cannot be found or one with a Generic Error message if a different type of error occurred (exception thrown, connection error, etc). I also need to log the exception where possible to a log/text file. If the Debug Mode is set, all I want to see is the Exception that occurred.
I've tried various methods, but I seem to have hit a dead end. I've tried using the ErrorHandler Plugin but it just show a blank page when an exception occurrs. I've tried using a custom Plugin just to handle 404 errors, doesn't really work because it conflicts with my Authentication Plugin. Tried without the Authentication Plugin, but then it gives issues because of my usage of Modules (just 1 actually, for Admin).
After trying various other things and combinations of them, I can't seem to figure out a way to achieve this.
I would greatly appreciate if anybody can shed some light as to how best to get the desired result.
I also have some assumptions that I would like clarified if I may. They are:
$oFrontController->throwExceptions(false);
Must be set to false in order for the ErrorHandler Plugin to work. Is this correct?
try {
$oFrontController-开发者_运维问答>dispatch();
} catch (Exception $oException) {
...
}
I have the above code to catch Exceptions thrown and log them, but as mentioned above, doesn't seem to do the job (works fine when I'm not using the ErrorHandler though, by setting $oFrontController->throwExceptions(true);).
What's the best way to handle Authentication? I currently have it set up as a Plugin, that sets up the 'resource' based on the module/controller and checks with a ACL class so as whether to give access or not. I've seen some examples where the ACL portion is separate on a controller basis. Would it be more sensible to have the Authentication as a Model that gets called at the initialisation of the controller and checks with it's respective set of permissions if the action should be allowed or not? (I ask this mainly because of the conflict that arose when trying to do error pages with the Authentication to see what alternatives are available)
Thank you for any help that you can give me on this matter. Thank you.
install the Exception Formatter Plugin and create an error controller
class ErrorController extends Zend_Controller_Action {
public function errorAction() {
$environment = Zend_Registry::getInstance()->configuration->environment;
if( $environment == 'development' ) {
// The ExpectionFormatter requires access to a couple of protected
// items so the must be passed in explicitly. The 3rd param
// controls whether or not the stack trace contains interactive source
// code. When enabled browser performance is degradded. It will default
// to false if the 3rd param is not provided
ExceptionFormatter::display(
$this->_getParam('error_handler'),
$this->_helper,
true
);
} else {
$errors = $this->_getParam('error_handler');
switch ($errors->type) {
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;
default:
// application error
$this->getResponse()->setHttpResponseCode(500);
$e = $this->_getParam('error_handler');
$this->view->message = $e['exception']->getMessage();
break;
}
$this->view->environment = $environment;
$this->view->exception = $errors->exception;
$this->view->request = $errors->request;
}
}
}
精彩评论