Welcome Guest, Not a member yet? Register   Sign In
How to detect if a class/method exists before it's called by CI?

I'm working on the next update for Ocular, and am wanting to make it automatically check for existing view files even when a controller doesn't exist. The problem is that I'm not sure how to do this...

My initial thought is to hook into pre_controller(). However, the router has already run, so it should be kicking up an error at that point. Is this correct?

Does anyone have any other suggestions?


Check out the _remap() function (about halfway down that page).

as ucantblamem indicates we have used the _remap() function to display a view if no method exists in the controller
if however you want to display something if no controller exists you do not have many options

I am not sure a pre_system hook would be much use because almost nothing is available at that point to determine what is going on. Router is only created after that so you would have to determine the controller yourself.
You could extend he router class and if after it is constructed you could check to see if the controller file exists and if not do whatever you want

Not sure there is any other way you could do it

I've used the _remap function before, and am very fond of it actually. But, like tonanbarbarian says, I'm looking to basically capture the error if no controller exists, and then send it to my own routine. Actually, it's going to have 2 separate uses, though they're related:

1) If not controller exists, the library will check to see if a view for that controller/function exists and display it as normal, and

2) Eventually, allow me to create a page for a path that doesn't exist, much like a wiki.

I'll look into the router class some more, and see what it would take to override it.

Thanks, all.

I've figured out a partial way to do this. For the wiki-type functionality, it looks like it's possible to just override the show_404() function in the Exceptions library. Make it check to see if the app is using Ocular Pages and, if so, tell it do display that default view. If not using the Pages functionality, then proceed as normal.

I think this will actually work with showing view for views that don't exist, also, but I haven't had a chance to dig into the code and make sure all of the required routing information is still available, though I think it should be.

The file you really should be working with is CodeIgniter.php - this is the front controller - kind of - it isn't an object - GASP - it contains the dispatch process and this is what you need.

With all respect to the dev team, it is kind of ugly.

BTW, "pre_controller" happens after the place you want to hook in. Also, "pre_system" happens before the place you want to hook in. You will probably have to rewrite this file to do what you want. If I were you, I'd rewrite it as a proper object.

If you wanted a quick and dirty solution, you could create your own hook at line 82 - or maybe line 96 after the cache has run.

Theme © iAndrew 2016 - Forum software by © MyBB