debuggable

 
Contact Us
 

Dessert #8 - Render custom errors

Posted on 17/9/06 by Felix Geisendörfer

This is a very small but hopefully tasteful dessert. You've probably seen the /app/views/errors/ folder before and you maybe also know that you can use it to overwrite the default error templates which can be found in /cake/libs/view/templates/errors/ (error404.thtml, missing_action.thtml, missing_component_class.thtml, missing_component_file.thtml, missing_connection.thtml, missing_controller.thtml, missing_helper_class.thtml, missing_helper_file.thtml, missing_layout.thtml, missing_model.thtml, missing_scaffolddb.thtml, missing_table.thtml, missing_view.thtml, private_action.thtml, scaffold_error.thtml).

But what you might not know, or have not used before is that you can also use this folder to place your own error templates in it. For example if you have an action where the user needs to have a certain permission to do something and you want to render /app/views/errors/permission_denied.thtml when the permission isn't set, you can do it like that:

$this->viewPath = 'errors';
$this->render('permission_denied');

Now I've seen people using $this->render('../errors/permission_denied') before, but I think this is a more logical & cakish' way of doing it. It can also be used to render views from different controllers or elements, *but* I would not recommend you to do this unless you have a good reason to do so.

--Felix Geisendörfer aka the_undefined

 
&nsbp;

You can skip to the end and add a comment.

Moot  said on Apr 14, 2007:

Very helpful!

This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.