bleu
bleu

Reputation: 87

Redirecting errors in codeigniter

I want all codeigniter errors to be redirected to a custom error page. I have redirected 404 errors with $route['404_override'] = 'errorcontroller'; How can I redirect all the other errors such as db and php

Upvotes: 0

Views: 1467

Answers (1)

shawndreck
shawndreck

Reputation: 2069

You can always use try and catch block and then redirect to your custom error page. But as per your questions, there are some things you should consider.

  1. Redirect will issue a completely new request which is accomplished with the header sent to the browser. For that reason you will not be able to display the error message unless you save that message in session or something.

  2. Some errors results in PHP throwing exceptions while others don't. Some DB interactions error only raises PHP warnings or error. PHP execution may halt but no exception will be thrown. In such case even wrapping the code in try catch will not be any help. However, you can create a custom error handler and inform PHP to use that handler. Then in the custom error handler you are free to throw exceptions or redirect to error page.

  3. you can also use .htaccess to force redirection to the pages you set at specific header codes [404,304,500] etc

Hope this helps!

Upvotes: 1

Related Questions