This teknoid blog post describes an excellent method of refining Cake’s default error handling.
Quick reminder: by default, if your debug level is set to 0, cake will always redirect your user to the error404.ctp under app/views/errors).
Edit Jan 5, 2011: The information in this post is for Cake 1.2/1.3. See COTP’s comment below if you’re using Cake 2.0 or newer.
I recently had an issue with the 404 error page in an application I’m coding.
The application let’s a user log in to the system, and after a user logs in, there’s a box at the top right of the screen that displays the user’s name like this:
When I go to a non-existing URL, CakePHP redirects me to the error404.ctp page, as expected. However, as none of my controllers is involved (and especially not my app_controller which sets the logged in user name for default.ctp) – the login information does not exist, and my 404 page looks like this:
This saddened me to no end. Actually, it saddened my Business Analyst, and I do not like it when he’s unhappy. I therefore read and re-read the aforementioned teknoid’s post, until I found a silly hack that does the trick:
<?php
class AppError extends ErrorHandler {
function __construct($method, $messages) {
parent::__construct($method, $messages);
}
function _outputMessage($template) {
$this->controller->redirect('/error404');
$this->controller->afterFilter();
}
...
}
?>
All app_error.php does is take control of error handling from Cake’s default. All I do in it is redirect the incoming request to the /error404 path.
In routes.php, I connect /error404 to a specific controller/action pair:
Router::connect('/error404', array('controller' => 'bulls', 'action' => 'error'));
And in bulls_controller.php – just add an empty error function:
function error() {}
Last – add the error.ctp view:
<h1>404. Oops. Dais.</h1>
And that’s it! Now, when the user enters a non-existing URL, Cake is going to redirect it to the bulls controller. Because of this, I regain the user’s context in my error page!
(Note, there’s probably an easier way to do it, but the point is, you can use this technique whenever you set up some data for your default template that you wish was available for your error page as well).
Popularity: 5% [?]
3 Responses to CakePHP: Keeping login information on error pages
Leave a Reply Cancel reply
Recent Comments
- Philip: Look tried everything. Very new to cakephp. Is there any zipped file of this tuturial in its entirety. Really...
- milina: The problem was I haven’t import the “fullcalendar.js”. Since i’m new to the programming, can you...
- milina: There is no method call eventClick in fullcalendar-1.5.3. How do i handed this issue.
- Agus: private List<Program> programs = new ArrayList<Program>();
- Agus: Nice article. You may drop the Programs class (it’s of no use unless it has properties) thanks to the...
Categories
- Android (1)
- appengine (3)
- Be nice to your users (6)
- CakePHP (13)
- General (5)
- Googlemaps (3)
- Java (11)
- javaScript (4)
- jQuery (11)
- Seam (2)
- Security (4)
- Spring MVC (1)
- Spring Webflow (2)
- User Interface (5)
- Wordpress (2)
- Zope (1)
Archives
- February 2012 (1)
- January 2012 (2)
- December 2011 (1)
- July 2011 (2)
- June 2011 (4)
- May 2011 (1)
- December 2010 (3)
- November 2010 (1)
- August 2010 (2)
- July 2010 (2)
- June 2010 (4)
- May 2010 (4)
- April 2010 (5)
- March 2010 (3)


For CakePHP 2.0+, you can copy lib/Cake/Controller/CakeErrorController.php to app/Controller/CakeErrorController.php and use the usual $this->set() function.
Great post! Very informative
** Just to clarify, in CakeErrorController.php (the copy in app/Controller/)
parent::beforeRender();
$this->set('user_id', $this->Auth->user('id'));
foreach ($this->viewVars as $key => $value) {
if (!is_object($value)){
$this->viewVars[$key] = h($value);
}
}
}
@COTP – great, thanks! I edited to mention this is old Cake