Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom error pages with templates in CodeIgniter

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 :-)

like image 909
Anders Avatar asked Oct 04 '11 06:10

Anders


People also ask

How to create custom 404 page in CodeIgniter?

There is two ways for creating a custom 404 error page with CodeIgniter. The first one is simply to customize the file /application/views/errors/html/error_404. php. Modify this file and replace his content with your custom page.

How to enable error reporting in CodeIgniter?

By default, CodeIgniter displays all PHP errors. You might wish to change this behavior once your development is complete. You'll find the error_reporting() function located at the top of your main index. php file.

How to solve 404 error in CodeIgniter?

First and foremost a main. php file can cause this error. Actually, this file must be present under the controllers' folder and must have a class Main which extends CI_Controller as shown below.


2 Answers

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 the show_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.

<?php

if(!(isset($UserIsLoggedin))){
  $this->load->view('template/header');
  show_error('User is not logged in', 401);
  $this->load->view('template/footer');
}

?>

Your error_401.php file should than look like this:

<div id="container">
	<h1><?php echo 'This is an 401 error'; ?></h1>
	<?php echo $message; ?>
</div>

/application/core/MY_Exceptions.php:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Exceptions extends CI_Exceptions
{
	
	function show_error($heading, $message, $template = 'error_general', $status_code = 500)
	{		
		if((!isset($template)) || ($template == 'error_general')){
			if(file_exists(APPPATH.'errors/error_'.$status_code.'.php')) {
				$template = 'error_'.$status_code;
			}
		}    	
		
		if (!isset($status_code)) $status_code = 500;
		
		set_status_header($status_code);

		$message = '<p>'.implode('</p><p>', ( ! is_array($message)) ? array($message) : $message).'</p>';

		if (ob_get_level() > $this->ob_level + 1)
		{
			ob_end_flush();
		}
		ob_start();		
				
		include(APPPATH.'errors/'.$template.'.php');
		$buffer = ob_get_contents();
		ob_end_clean();
		return $buffer;
	}
	
}

?>
like image 99
Corné Schot Avatar answered Sep 30 '22 14:09

Corné Schot


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:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Site extends CI_Controller {

    public function index()
    {
        $this->load->view('welcome_message');
    }

    public function view($page = "home" , $function = "index")
    {
        do_something();
        if($status == "404")
        {
            $function = "404";
        }
        $this->load->view('templates/header', $data);
        $this->load->view($page.'/'.$function, $data);      
        $this->load->view('templates/footer', $data);
    }




}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

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.

like image 33
jeff Avatar answered Sep 30 '22 13:09

jeff