![]() |
Custom Exception & get_instance() - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Custom Exception & get_instance() (/showthread.php?tid=67859) |
Custom Exception & get_instance() - petewulf1 - 04-18-2017 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 RE: Custom Exception & get_instance() - dave friend - 04-18-2017 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? RE: Custom Exception & get_instance() - petewulf1 - 04-18-2017 (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 That's all. Code: get_instance(); has no effect because CI is not loaded at this point i think. Regards, Daniel RE: Custom Exception & get_instance() - dave friend - 04-18-2017 (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 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')) 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($page, TRUE, $this); Does that help? RE: Custom Exception & get_instance() - petewulf1 - 04-19-2017 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 |