CodeIgniter 2.0 was released a few days ago, and one of the (much overdue) features I’m looking forward to using is the ability to define a custom 404 controller. This is useful for handling requests to non-existant controller methods by specifying a controller to call instead. Here’s how to get it setup:
You should now have a functional error controller for 404s when triggered by a request for non-existent controller methods. But what if you want to trigger 404s from your code and use the error controller?
Something like this, perhaps?
Unfortunately, this will still use the built-in non-controller error message. I got to playing around and discovered that (at least in CI 2.0, maybe 1.7.2) you can do this:
Of course you could do the same thing for other error types (403, 500, etc) by creating new methods in the Error controller and calling the correct method. It could even be simplified more by creating a helper with a function such as this one: