I'm using the template library for CodeIgniter, http://williamsconcepts.com/ci/codeigniter/libraries/template/reference.html, and now I want to implement custom error pages too. I found one method involving a MY_Router extending the default router: http://maestric.com/doc/php/codeigniter_404 but that only treats 404 errors. I want all errors to show a simple user-friendly page, including database errors etc, and I want it to go through a controller, partly so I can use the template library, and partly so I can also implement an email function to send myself information about the error that occurred.
Someone asked about extending the functionality of the above MY_Router method for other errors, like error_db, but got no answer from the author, so I'm turning here to see if anyone knows how to do this, along the lines of the above method or any other simple way of achieving it. Please note that I'm a newbie, so do not assume too much about my knowledge of basic CodeIgniter functionality :-)
I've created an extension for the Exceptions class. In this extension I've replaced the
$this->Exceptions->show_error();
method, witch is used by theshow_error()
function of CI.when I call
show_error('User is not logged in', 401);
this custom method is looking for an error_$status_code
file first. In the case of the example above, it will look for an error_401.php file.When this file does not exists, it wil just load the error_general.php file, like the default
$this->Exceptions->show_error();
does.In your case, you can use the following code to use in your library, controller or whatever should throw an error.
Your error_401.php file should than look like this:
/application/core/MY_Exceptions.php:
edit
type here your controller for example Error
create a function index and load your view
I do it like this: I create my own error page, and whenever I should throw a 404 error, I actually load my 404 page.
So say my default controller is site.php, my site.php looks like this:
So I serve the home/404.php whenever an error occurs. i.e., I don't allow CodeIgniter to call
show_404();
therefore the 404 page looks like any other page.p.s. I assume that you followed the nice tutorial in CodeIgniter's website.
The simplest way to create custom error pages is to edit the files at
/application/views/errors/html/error_*.php
such aserror_404.php
(for 404s),error_db.php
(for database errors) anderror_general.php
(for most other errors).As these pages are within your
application
directory, you are free to customise them to your needs.If your normal view template looks something like this:
You can adapt this in your
/application/views/errors/html/error_*.php
files like so:Notice that we're no longer using views, but instead including the view files for the
header
&footer
.Another thing to note:
In the
header
view, I'm passing a$data
object which includes$data['page_title']
. As the error pages don't use views, you have to add any variables that you'd normally pass into the view, hence the presence of$page_title
.