Chopra
Chopra

Reputation: 571

Custom view in show_404() not working

I have tried both 404_override and MY_Exceptions for showing custom error view. Can somebody please help me with it?

Here is the 404_override code

Routes.php

$route['404_override'] = 'error/index';

Error.php

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

class Error extends CI_Controller {

    public $user_id ='';

    function __construct()
    {
        parent::__construct();
        $this->load->library('login.php');
        $user_Details = $this->login_library->get_user_details();
        $this->user_id = $user_Details['user_id'];
    }

    public function index()
    {
        $this->output->set_status_header('404');
         $this->output->set_status_header('404');  
        $this->load->view('view_header',$this->user_id); 
        $this->load->view('view_404');
        $this->load->view('view_footer',$this->user_id);     
    }



}

Here is the My_Exceptions code

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

class MY_Exceptions extends CI_Exceptions{

    function MY_Exceptions(){
        parent::CI_Exceptions();
        $this->CI =& get_instance();
    }

    function show_404($page=''){ 
        echo $heading = "404 Page Not Found";
        echo $message = "The ppage you requested was not found.";
        $this->config =& get_config();
        $baseUrl = $this->config['base_url'];
        header("location: ".$baseUrl.'error.html');
        exit;
    }

}

Upvotes: 4

Views: 15544

Answers (6)

Muyin
Muyin

Reputation: 123

You have to edit the core file of CodeIgniter to use custom 404 pages through show_404() function. Just go to \system\core\Common.php and replace show_404() function with following code:

function show_404($page = '', $log_error = TRUE)
{
     $ci = get_instance();
     $ci->output->set_status_header(404);
     $ci->load->view('404');
     echo $ci->output->get_output();
     exit(4);
}

Note: Please replace Your_custom_404_page text with your custom 404 page.

It's working properly on my own Codeigniter Application. I hope it will work properly

Upvotes: 0

Kerkouch
Kerkouch

Reputation: 1456

I'm too late to answer this question, but maybe that will help anyone else looking for a solution to this problem.

I tried the answer proposed by Chopra but it's not working, because the show_404 signature must be same as the one it overrides.

I have replaced the core class CI_Exceptions with that new one.

<?php
// application/core/MY_Exception.php
class MY_Exceptions extends CI_Exceptions
{
    /**
     * Show a custom 404 error
     */
    public function show_404($page = '', $log_error = TRUE)
    {
        $CI =& get_instance();
        $data['title'] = '404 Page Not Found!';
        $output = '';
        $output .= $CI->load->view('templates/header', $data, true);
        $output .= $CI->load->view('error_404', '', true);
        $output .= $CI->load->view('templates/footer', $data, true);
        set_status_header(404);
        echo $output;
        exit;
    }
}

Upvotes: 0

MAYOBYO HASSAN
MAYOBYO HASSAN

Reputation: 506

i had a similar problem and tried extending the Exceptions class, but codegniter wasn't supporting extending the Exceptions class

This is what worked for me, in order to override the show_404() method

create a helper method

if ( ! function_exists('custom_show_404'))
{
    /**
    * Method Name custom_show_404
    *
    */
    function custom_show_404()
    {
        $CI =& get_instance();
        $CI->load->view("not_found_page");

        //log message
        log_message('error','PAGE NOT FOUND ETC...');
    }
}

after that find and replace all show_404() method calls with custom_show_404() in the application/controllers folder

Note: don't forget to load the helper file that contains your new method

Upvotes: 0

Abin John
Abin John

Reputation: 381

If you want to show custom 404 template

You need to change $route['404_override'] = 'welcome/page_not_found'; in config/routes.php file.

and write the action in controller

public function page_not_found(){
    $this->data['page_title'] = 'Page Not Found!';
    $this->load->view('layouts/page_not_found.php', $this->data);
}

Important:Your page_not_found action should be in your default controller (You can set default controller from config/routes.php, $route['default_controller'] = 'welcome';)

Upvotes: 3

Chopra
Chopra

Reputation: 571

I got the solution from this link - CodeIgniter 2.1 issue with show_404() and 404_override.

<?php
// application/core/MY_Exceptions.php
class MY_Exceptions extends CI_Exceptions {

    public function show_404()
    {
        $CI =& get_instance();
        $CI->load->view('my_notfound_view');
        echo $CI->output->get_output();
        exit;
    }
}

Thanks a lot for all the help. Appreciated :)

Upvotes: 13

Pixel
Pixel

Reputation: 1976

Try to changing the config.php to

$config['uri_protocol'] = "REQUEST_URI";

it work ?.

Upvotes: 0

Related Questions