• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Custom Error function - Extend Exceptions or add to Common? Confused

#4
[eluser]the real rlee[/eluser]
For anyone referring to this thread here's how i created my custom error:

Extended Exceptions Library (My_Exceptions) (CI uses this to create its own global show_error(), show_404() functions)
Code:
class MY_Exceptions extends CI_Exceptions {

    function MY_Exceptions(){
        parent::CI_Exceptions();
    }
    
    function show_401()
    {    
        $heading = "401 Not Authorized";
        $message = "You are not authorized to view this page.";
        
        echo $this->show_error($heading, $message, 'error_401');
        exit;
    }
    
}

Created a helper (helpers/error_helper.php) for my custom function which i autoload (CI does this within CodeIgniter/Common)
Code:
function show_401(){
    $exceptions =& load_class('Exceptions');
    $exceptions->show_401();
    
}

Created by own errors page (applications/errors/error_401.php)

Code:
<?php header("HTTP/1.1 401 Unauthorized"); ?>
<html>
<head>
<title>401 Unauthorized</title>
<style type="text/css">

body {
background-color:    #fff;
margin:                40px;
font-family:        Lucida Grande, Verdana, Sans-serif;
font-size:            12px;
color:                #000;
}

#content  {
border:                #999 1px solid;
background-color:    #fff;
padding:            20px 20px 12px 20px;
}

h1 {
font-weight:        normal;
font-size:            14px;
color:                #990000;
margin:             0 0 4px 0;
}
</style>
</head>
<body>
    <div id="content">
        <h1>&lt;?php echo $heading; ?&gt;</h1>
        &lt;?php echo $message; ?&gt;
        <p>&lt;?=anchor('user/login', 'Please login to continue')?&gt;</p>
    </div>
&lt;/body&gt;
&lt;/html&gt;

And hey presto (example usage):

Code:
Admin extends Controller {

   function admin(){
      parent::Controller();

      if (!$this->user_auth->is_logged_in()) {
         show_401();
      }
    
   }
}


Messages In This Thread
Custom Error function - Extend Exceptions or add to Common? Confused - by El Forum - 08-01-2007, 06:04 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.