Welcome Guest, Not a member yet? Register   Sign In
How to load a view for the error page?
#26

[eluser]Aquillyne[/eluser]
Okay the fixed code that even works on 404's.

I've used CI sessions where possible, but only in the case where CI isn't yet instantiated (404 errors), it resorts to native PHP sessions.

MY_Exceptions.php

Code:
class MY_Exceptions extends CI_Exceptions {

    var $CI;
    
    function MY_Exceptions()
    {
        parent::CI_Exceptions();
    }
    
    function _redirect($error)
    {
        if (function_exists("get_instance"))
        {
            $this->CI =& get_instance();
            
            $err = $this->CI->session->flashdata("error");
            if ($err != FALSE)
            $this->show_fatal_error($err["heading"], $err["message"]);
            
            $this->CI->session->set_flashdata("error", $error);
            redirect("error");
        }
        else
        {
            session_start();
            $_SESSION["error"] = $error;
            
            require(APPPATH . "config/config.php");
            $config["index_page"] == "" ? $slash = "" : $slash = "/";
            $redir = $config["base_url"] . $config["index_page"] . $slash . "error";
            
            header("Location: $redir");
            
            $this->show_fatal_error($error["heading"], $error["message"]);
        }
    }
    
    function show_fatal_error($heading = "Fatal Error", $msg = "Unknown Error")
    {
        die("<h1>$heading</h1>" . $msg);
    }
    
    function show_error($heading = "Fatal Error", $message = "Unknown Error", $template = "general")
    {
        $error = array(
            "heading" => $heading,
            "message" => $message,
            "template" => $template
        );
        $this->_redirect($error);
        return FALSE;
    }
    
    function show_php_error($severity, $message, $filepath, $line)
    {
    $severity = ( ! isset($this->levels[$severity])) ? $severity : $this->levels[$severity];
        
    $filepath = str_replace("\\", "/", $filepath);
    if (FALSE !== strpos($filepath, '/'))
    {
        $x = explode('/', $filepath);
        $filepath = $x[count($x)-2].'/'.end($x);
    }
        
    $heading = "Error ($severity)";
    
    $message = $message . " in file " . $filepath . " on Line " . $line . ".";
        $this->show_error($heading, $message);
    }
    
    function show_404($page = "")
    {    
        $heading = "404 Page Not Found";
        $message = "The page you requested was not found.";
        log_message('error', '404 Page Not Found --&gt; '.$page);
        $this->show_error($heading, $message, "404");
    }
}

This also requires something special in the error controller.

error.php in controllers folder

Code:
class Error extends Controller {

    var $err;

    function Error()
    {
        parent::Controller();
        $this->err = $this->session->flashdata("error");
        if ($this->err == FALSE)
        {
            session_start();
            if (isset($_SESSION["error"]))
            {
                $this->err = $_SESSION["error"];
                unset($_SESSION["error"]);
                session_destroy();
            }
            else
            {
                redirect();
                die("This page cannot be accessed directly.");
            }
        }
    }

    function index()
    {

        $data = array(
            "heading" => $this->err["heading"],
            "message" => "Message: " . $this->err["message"]
            );
        
        $this->load->view('error_view', $data);
        
    }

}


Messages In This Thread
How to load a view for the error page? - by El Forum - 06-07-2008, 08:57 PM
How to load a view for the error page? - by El Forum - 06-08-2008, 04:07 AM
How to load a view for the error page? - by El Forum - 06-08-2008, 07:04 AM
How to load a view for the error page? - by El Forum - 06-08-2008, 07:43 AM
How to load a view for the error page? - by El Forum - 06-08-2008, 11:28 AM
How to load a view for the error page? - by El Forum - 06-08-2008, 12:18 PM
How to load a view for the error page? - by El Forum - 06-08-2008, 12:21 PM
How to load a view for the error page? - by El Forum - 07-19-2008, 12:32 PM
How to load a view for the error page? - by El Forum - 07-19-2008, 01:48 PM
How to load a view for the error page? - by El Forum - 07-19-2008, 03:04 PM
How to load a view for the error page? - by El Forum - 07-19-2008, 03:25 PM
How to load a view for the error page? - by El Forum - 07-20-2008, 10:16 AM
How to load a view for the error page? - by El Forum - 07-20-2008, 10:23 AM
How to load a view for the error page? - by El Forum - 07-20-2008, 10:28 AM
How to load a view for the error page? - by El Forum - 07-20-2008, 10:30 AM
How to load a view for the error page? - by El Forum - 07-20-2008, 10:31 AM
How to load a view for the error page? - by El Forum - 07-20-2008, 10:33 AM
How to load a view for the error page? - by El Forum - 07-20-2008, 10:42 AM
How to load a view for the error page? - by El Forum - 07-20-2008, 10:44 AM
How to load a view for the error page? - by El Forum - 07-20-2008, 10:58 AM
How to load a view for the error page? - by El Forum - 07-20-2008, 11:09 AM
How to load a view for the error page? - by El Forum - 07-20-2008, 11:12 AM
How to load a view for the error page? - by El Forum - 07-20-2008, 12:21 PM
How to load a view for the error page? - by El Forum - 07-20-2008, 01:11 PM
How to load a view for the error page? - by El Forum - 07-20-2008, 01:15 PM
How to load a view for the error page? - by El Forum - 07-20-2008, 05:46 PM



Theme © iAndrew 2016 - Forum software by © MyBB