How do I create a decent error 404 handler for CodeIgniter? How do I create a decent error 404 handler for CodeIgniter? codeigniter codeigniter

How do I create a decent error 404 handler for CodeIgniter?


I use CodeIgniter with Smarty. I have an additional function in my Smarty class called notfound(). Calling notfound() sets the correct header location to a 404 page and then displays a 404 template. The template has an overrideable title and message so it's very versatile. Here's some sample code:

Smarty.class.phpfunction not_found() {header('HTTP/1.1 404 Not Found');if (!$this->get_template_vars('page_title')) {    $this->assign('page_title', 'Page not found');    }    $this->display('not-found.tpl');    exit;}

In a controller I can do something like this:

$this->load->model('article_model');$article = $this->article_model->get_latest();if ($article) {    $this->smarty->assign('article', $article);    $this->smarty->view('article');} else {    $this->smarty->assign('title', Article not found');    $this->smarty->not_found();}

Likewise, I can change the code in /system/application/error/error_404.php to:

$CI =& get_instance();$CI->cismarty->not_found();

It works great, uses a small amount of code, and doesn't duplicate 404 functionality for different types of missing entities.

I think you could do a similar thing with the built-in CodeIgniter views. The important thing is to spit out the header before you do your view stuff.

Update: I use a custom Smarty wrapper similar to the one described here:

Using Smarty with CodeIgniter