Porting Agavi’s shiny error handler to Zend Framework
Last Tuesday I joined the Webtuesday Zurich meeting. David Zülke (project lead, Munich) gave a talk on Agavi Framework.
Agavi is a general purpose PHP application framework built around the Model-View-Controller architecture originally based on the Mojavi 3 Web application framework written by Sean Kerr. It provides a rich toolset that solves most of the routine problems in Web application development. (…)
There are some great ideas and programming techniques behind Agavi and a very bright engineering team. Congrats, David, that was a great presentation of a great project! Agavi is not yet well known in PHP community but you definitely should give it a try! There’s a well though-out architecture behind it.
There will be a webcast of David’s presentation available soon on Webtuesday Zurich.
Well, I’m going to stick with Zend Framework for the near future. One tiny little thing that bothered me in Zend Framework was the fact, that I had to write my own ErrorController and error view script. It can be done in a pretty easy way by following the instructions in the ZF Quick Start. But I would have preferred this to be integrated in a nice way like in Agavi (introduced in 0.11, “shiny” exception template). When I saw its exception handler template I was simply astonished! Wow!
A drop dead gorgeous exception template with eye candy embedded as inline SVG
Check this out: http://zfskeleton.onlime.ch/index/foo

Porting Agavi’s shiny exception template to Zend Framework was a piece of cake. I grabbed the template from Agavi 1.0.1 (located in /src/exception/templates/shiny.php) and made some tiny modifications.
All you need is an ErrorController and the modified shiny.phtml view script. You can grab both from our Subversion repository:
svn co https://svn.onlime.ch/public/zfskeleton/trunk zfskeleton
zfsekeleton is our Zend Framework “Hello World” demo application. It’s built on Zend Framework 1.8.x best practices and uses Zend_Application. Below you’ll find our ErrorController.
Have fun!
/** * ErrorController * * @author Philip Iezzi * @copyright Authors * @copyright Copyright (c) 2007-2009 Onlime Webhosting, www.onlime.ch */ class ErrorController extends Zend_Controller_Action { /** * errorAction() is the action that will be called by the "ErrorHandler" * plugin. When an error/exception has been encountered * in a ZF MVC application (assuming the ErrorHandler has not been disabled * in your bootstrap) - the Errorhandler will set the next dispatchable * action to come here. This is the "default" module, "error" controller, * specifically, the "error" action. These options are configurable, see * the docs on the ErrorHandler Plugin * * @return void */ public function errorAction() { // Ensure the default view suffix is used so we always return good // content $this->_helper->viewRenderer->setViewSuffix('phtml'); // use shiny exception handler view, if configured as: // resources.frontController.errorview = shiny if ($this->getInvokeArg('errorview') && $this->getInvokeArg('errorview') != 'error') { $this->_helper->layout->disableLayout(); $this->_helper->viewRenderer($this->getInvokeArg('errorview')); } // Grab the error object from the request $errors = $this->_getParam('error_handler'); // $errors will be an object set as a parameter of the request object, // type is a property 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 = '404 Page not found'; break; default: // application error $this->getResponse()->setHttpResponseCode(500); $this->view->message = '500 Application error'; break; } // pass the environment to the view script so we can conditionally // display more/less information $this->view->env = $this->getInvokeArg('env'); // pass the actual exception object to the view $this->view->exception = $errors->exception; // pass the request to the view $this->view->request = $errors->request; } }
























Chrigu said
am June 17 2009 @ 12:34 pm
Sweet!
Sebastian said
am August 5 2009 @ 7:31 pm
Great! Thank you very much for sharing this fine peace of code!
Best regards
Sebastian Fahrenkrog
Udo Schochtert said
am September 11 2009 @ 10:19 am
“A drop dead gorgeous exception template” describes it quite well!
Thanks for adapting and sharing!
Small improvement:
You should mention to add to the application.ini file
; set error controller view to shiny
resources.frontController.errorview = shiny
This would also be the place where you would easily reenable the default error view “error”.
Best, Udo
NorthernLight said
am November 16 2009 @ 11:24 am
Very good error template, I really like it, but one little thing, it raise an exception in some browser because it could generate a non valid Xml document error.
To prevent this to happend, simply replace this line:
by this line:
In the original line, $requestParams is printed without being escaped by htmlspecialchars and can result to generating non valid XML.
Great work overall,
Best
NorthernLight said
am November 16 2009 @ 11:26 am
Add with escaped code
Replace:
<?php echo htmlspecialchars(print_r($requestParams)); ?>
by
<?php echo htmlspecialchars(print_r($requestParams, true));
?>
I hope it’ll display this time…
christian said
am November 28 2009 @ 8:11 pm
oh wow! that is absolutely amazing! Thanks man! Great work
Greg said
am November 29 2009 @ 12:17 am
It’s really great but it doesn’t go with ZFDebug.
Anyone has tried to solve this conflict?
Csiszár Attila said
am November 29 2009 @ 1:55 pm
Thanks! Its a really fantastic errorview!
However I changed it a little to output different content in production and development mode.
@greg: I tried it. The fact is that ZFdebug html output generate some non valid xhtml tags and this causes xml rendering error in Firefox.
The latest ZFDebug source code (checked out from svn) seem to be solve this issues however first you have to switch your view’s doctype to xhtml.
In your Zend Application bootstrap:
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(‘viewRenderer’);
$viewRenderer->initView();
$viewRenderer->view->doctype(‘XHTML1_TRANSITIONAL’);
I tried to use this snippet only in ErrorController, but i have no success. I think this is related to this bug:
http://www.zendframeworkinaction.com/2008/02/24/careful-where-you-set-your-doctype/
Greg said
am November 29 2009 @ 11:50 pm
@Csiszár I tried it to no avail
still parsing error.
Greg said
am November 30 2009 @ 1:08 am
I solved it at last! Great!
Juan Felipe Alvarez Saldarriaga said
am November 30 2009 @ 2:44 pm
Great! looks so pretty ^_^
iezzip said
am November 30 2009 @ 8:46 pm
@NorthernLight: Thanks for the bug report.
fixed in r3066
Checkout my short documentation (in German) about the zfskeleton demo-app:
http://wiki.onlime.ch/dev:zf#demo-applikation
If you don’t like to grab my code from Subversion, you can download the full project: zfskeleton.zip
And for those who are still looking for a good hosting provider that does support latest PHP 5.3 versions and Zend Framework, checkout Onlime Webhosting (Switzerland):
Zend Framework – mega zestawienie : W drodze ku szczęściu said
am January 5 2010 @ 8:36 am
[...] Piękny error controller – http://www.iezzi.ch/archives/397 [...]
Beautify Zend Framework project error pages – Elink Media said
am February 6 2010 @ 7:21 am
[...] http://www.iezzi.ch/archives/397 Share this article on: [...]
bsagols said
am February 20 2010 @ 10:54 am
Wow! So nice… Great work, really. It helps me a lot while developing.
Thanks for sharing this.
PS: I have a XML parsing error some times though. I’ve disabled svg ($svg=false) always to fix this. Worth to know.
Frank said
am March 20 2010 @ 1:14 pm
Pretty cool, but is it possible to replace the SVG graphics with normal ones? The doctype is causing some issues.
ZFDebug for instance is unable to work due to this error.
Zend Framework – INICIO « Oscar Viana's Weblog said
am May 10 2010 @ 5:51 pm
[...] Piękny error controller – http://www.iezzi.ch/archives/397 [...]