By default CodeIgniter handles broken links(404), but when it comes to look and feel the default design becomes unpleasant. In CodeIgniter we can override the default 404 functionality and implement our own custom 404 page very easily. The steps are described below:
Step 1: First create a new controller to handle the 404 page requests. For that just create a new controller under your controllers directory. For example, Say 'custom404Page'.
The controller would look like this
class custom404Page extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function index(){
$this->output->set_status_header('404');
$data['errorMessage'] = ' Ooops! Page not found';
$this->load->view('custom404View',$data);//loading view
}
}
Step 2: Open routes.php under /application/config/ directory.
Step 2: Find the line which looks like $route['404_override'] = ''. If you cannot find such a line don't worry just add the above code in that file and point your controller file. For example in our case the code will be as below:
$route['404_override'] = 'custom404Page'
Step 1: First create a new controller to handle the 404 page requests. For that just create a new controller under your controllers directory. For example, Say 'custom404Page'.
The controller would look like this
class custom404Page extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function index(){
$this->output->set_status_header('404');
$data['errorMessage'] = ' Ooops! Page not found';
$this->load->view('custom404View',$data);//loading view
}
}
Step 2: Find the line which looks like $route['404_override'] = ''. If you cannot find such a line don't worry just add the above code in that file and point your controller file. For example in our case the code will be as below:
$route['404_override'] = 'custom404Page'
Step4: Now create the view with your design and its all set and ready to roll.
Another way for handling broken links is to override the Exception core class in CodeIgniter. There is a function called show_404() in CodeIgniter. You can either follow the method described in the above link or follow the following method.
Step 1: Open config.php under /application/config/ directory.
Step 2: Ensure the following line is present in config.php
$config['subclass_prefix'] = 'MY_';
Step 3: Now create a new file under /application/libraries/. Say MY_Exception and put the following code in it:
class MY_Exceptions extends CI_Exceptions{
function MY_Exceptions(){
parent::CI_Exceptions();
}
function show_404($page=''){
$this->config =& get_config();
$baseUrl = $this->config['base_url'];
header("location: ".$baseUrl.'error.html');//view file location
/* Use this section if your view file is under views directory
$CI = &get_instance();
$CI->load->view('custom404View', $data);
*/
exit;
}
}
Step 4: Now you can use show_404 function in any of your controllers. for example
class test extends CI_Controller{
public function index(){
if(some condition){
//do something
}else{
show_404();
}
}
}
Another way for handling broken links is to override the Exception core class in CodeIgniter. There is a function called show_404() in CodeIgniter. You can either follow the method described in the above link or follow the following method.
Step 1: Open config.php under /application/config/ directory.
Step 2: Ensure the following line is present in config.php
$config['subclass_prefix'] = 'MY_';
Step 3: Now create a new file under /application/libraries/. Say MY_Exception and put the following code in it:
class MY_Exceptions extends CI_Exceptions{
function MY_Exceptions(){
parent::CI_Exceptions();
}
function show_404($page=''){
$this->config =& get_config();
$baseUrl = $this->config['base_url'];
header("location: ".$baseUrl.'error.html');//view file location
/* Use this section if your view file is under views directory
$CI = &get_instance();
$CI->load->view('custom404View', $data);
*/
exit;
}
}
Step 4: Now you can use show_404 function in any of your controllers. for example
class test extends CI_Controller{
public function index(){
if(some condition){
//do something
}else{
show_404();
}
}
}