CodeIgniter Forums

Full Version: Custom Exception & get_instance()
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello guys,

i was searching for hours to find a solution to generate usable 404 errors in CI, but i think this section is not well integrated in CI:

When extending the CI Exception class, there is no way to get the instance with "get_instance()" because as what i've seen, CI isn't loading the core before the Exception controller. 
I need some basic functions to generate my 404 error pages, so this behavious is completely useless for me as it only handles static output.

If i use the method with 404_override in the config file, it works well, i can use the whole CI core functions but it is also not thought to end because sometimes i need to call show_404() manually which shows the ugly default error from the CI Exception class which leads to the problems above.

So there are 2 ways of integrating, but none of them is really usable. 
Sounds like a joke to me...

Has anyone solved this problem without modifying the core? 

Thanks in advance,
Daniel
Will you define "usable 404 errors" please. You clearly have something specific in mind but you don't explain what that is.

Quote:sometimes i need to call show_404() manually which shows the ugly default error from the CI Exception class

Wouldn't extending CI_Exceptions allow you to redefine CI_Exceptions->show_404() and/or CI_Exceptions->show_error() to produce the results you have in mind? If not, why not?
(04-18-2017, 07:45 AM)dave friend Wrote: [ -> ]Wouldn't extending CI_Exceptions allow you to redefine CI_Exceptions->show_404() and/or CI_Exceptions->show_error() to produce the results you have in mind? If not, why not?

Hi Dave,

thanky for your reply - surely i can extend the "CI_Exceptions->show_404()" with my own method. 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 can get the current instance, which brings up just the following class members:


Code:
Custom_Exceptions Object
(
   [ob_level] => 1
   [levels] => Array
       (
           [1] => Error
           [2] => Warning
           [4] => Parsing Error
           [8] => Notice
           [16] => Core Error
           [32] => Core Warning
           [64] => Compile Error
           [128] => Compile Warning
           [256] => User Error
           [512] => User Warning
           [1024] => User Notice
           [2048] => Runtime Notice
       )

)


That's all. 


Code:
get_instance();

has no effect because CI is not loaded at this point i think.

Regards,
Daniel
(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?
Yes i need the fully instantiated framework because i need the database ans some models to show additional information to the user, based on the url input.
Thanks for the tip with the helper function, this was exactly the solution i came up yesterday. This is a good workaround!
Regards,
Daniel