Custom view in show_404() not working

14,375

Solution 1

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

Solution 2

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';)

Share:
14,375

Related videos on Youtube

Chopra
Author by

Chopra

Web Developer, trekker, Foodie

Updated on September 14, 2022

Comments

  • Chopra
    Chopra over 1 year

    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;
        }
    
    }
    
  • Growiel
    Growiel over 7 years
    It doesn't. It overrides it for custom behavior, this is comonly done.