Welcome Guest, Not a member yet? Register   Sign In
Custom Exception & get_instance()
#4

(04-18-2017, 08:48 AM)petewulf1 Wrote: The Problem is, that the whole CI-Exception class works like a standalone class without any CI core functionality like loading models, helpers and so on.

I get that. What I don't get is what you need from the core for your 404 page. Do you need the instance while the framework is in bootstrap phase - IOW, while executing CodeIgniter.php?

If your "need to call show_404() manually" happens after the framework is up and running then a custom 404 method in your extension of CI_Exception could be devised that will maybe do the trick.

PHP Code:
class MY_Exceptions extends CI_Exceptions
{

 
   /*
     * Accepts a third argument that would typically be '$this' when called from a running CI instance
     */
 
   public function show_custom_404($page ''$log_error TRUE$sender null)
 
   {
 
       if($sender)
 
       {
 
           //do stuff with the framework
 
           $sender->load->helper('url');
 
           $sender->load->view('my_swell_404_view');
 
       }
 
       else
        
{
 
           parent::show_404($page$log_error); //use the base class method
 
       }
 
   }


You would need to create a helper to allow a simple function call in the way show_404() is used.
File: application/helpers/custom404_helper.php

PHP Code:
if ( ! function_exists('show_custom_404'))
{
 
/**
 * A custom 404 Page handler that accepts a third argument
 *
 * @param string
 * @param bool
 * @param object An instance of CI ($this)
 * @return void
 */
 
function show_custom_404($page ''$log_error TRUE$sender NULL)
 {
 
$_error =& load_class('Exceptions''core');
 
$_error->show_custom_404($page$log_error$sender);
 exit(
4); // EXIT_UNKNOWN_FILE
 
}


So within some controller or other place in a fully instantiated framework (a place where $this is an instance of $CI)...

PHP Code:
show_custom_404($pageTRUE$this); 

Does that help?
Reply


Messages In This Thread
Custom Exception & get_instance() - by petewulf1 - 04-18-2017, 04:21 AM
RE: Custom Exception & get_instance() - by dave friend - 04-18-2017, 10:53 AM



Theme © iAndrew 2016 - Forum software by © MyBB