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

[eluser]Randy Casburn[/eluser]
@Aquillyne -- If you're using PHP v5 it may be a perfect opportunity for you to explore PHP's exception processing features. Just a thought...

Ramdy
#22

[eluser]Aquillyne[/eluser]
[quote author="Randy Casburn" date="1216591782"]@Aquillyne -- If you're using PHP v5 it may be a perfect opportunity for you to explore PHP's exception processing features. Just a thought...

Ramdy[/quote]

I am indeed using PHP 5, but I'd like to keep my whole application as compatible as possible. Thanks for the tip though!
#23

[eluser]Aquillyne[/eluser]
I've managed to get it working. You extend the Exceptions class as follows:

MY_Exceptions

Code:
class MY_Exceptions extends CI_Exceptions {

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

    function _redirect($error)
    {
        $err = $this->CI->session->flashdata("error");
        if ($err != FALSE)
        die("<h1>Fatal Error</h1>" . $err['message']);
        
        $this->CI->session->set_flashdata("error", $error);
        redirect("error");
    }

    function show_error($heading = "Fatal Error", $message = "Unknown Error")
    {
        $error = array(
            "heading"    => $heading,
            "message"    => $message
        );
        $this->_redirect($error);
    }
    
    function show_php_error($severity, $message, $filepath, $line)
    {
        $heading = "Error ($severity)";
        $message = $message . " in file " . $filepath . " on Line " . $line . ".";
        $this->show_error($heading, $message);
    }
    
}

Then you just need an error controller ("error"), which captures the session flashdata, and feeds it into an error view.

Note that I've got the sessions class set to autoload in my autoload.php config file.
#24

[eluser]Aquillyne[/eluser]
Ack; more problems.

The above code seems to work on anything but a 404 error. For this I get the error: "Call to undefined function get_instance()". Why is this?

As a side-note, I also get an extremely strange error for PHP errors. If my error message includes the filename I get an infinite redirection loop. If the message doesn't include the filename is works perfectly. What on earth?
#25

[eluser]Pascal Kriete[/eluser]
Most of the 404 errors get called before the base classes are loaded, so get_instance isn't defined yet. Also, the super object won't be instantiated yet. That's also the reason why I resorted to php sessions for my implementation.
#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);
        
    }

}




Theme © iAndrew 2016 - Forum software by © MyBB