Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
    }

}
like image 484
Chopra Avatar asked Dec 04 '22 07:12

Chopra


2 Answers

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

like image 160
Chopra Avatar answered Dec 28 '22 05:12

Chopra


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

like image 38
Abin John Avatar answered Dec 28 '22 07:12

Abin John