开发者

How do I override the not found page in CodeIgniter?

开发者 https://www.devze.com 2023-02-24 10:27 出处:网络
I have a CodeIgniter application that\'s generally working how I\'d like it to, but occasionally a user will go to a page that does not 开发者_运维技巧exist and is greeted with an unfriendly error. I\

I have a CodeIgniter application that's generally working how I'd like it to, but occasionally a user will go to a page that does not 开发者_运维技巧exist and is greeted with an unfriendly error. I'd like to detect the error automatically and display useful information to the user (not PHP errors). I read the user guide of CodeIgniter, but I couldn't find any relevant section.

How do I handle a page-not-found error in CodeIgniter and display my own custom content?


If you're looking at handling errors with your own custom page, you can modify the error templates found in application/errors. If you have a reason to based on your own code, you can manually send the user to one of these pages using show_404 or show_error - check out the Error Handling page in the official docs.


Try these codeigniter functions

show_404('Your error message');
show_error('Your error message');

you can find more detail at http://codeigniter.com/user_guide/general/errors.html

example:

if ($some_error) //condition
{
 show_error('Error');
}


You should test for error return values and catch exceptions. This is a general programming concept - not something specific to Conigniter or PHP.

Testing for error return values:

if (!sort($array))
{
    echo "Could not sort $array.";
}

Catching exceptions:

try
{
    $someFunction($data);
}
catch (Exception $e)
{
    echo "Something went wrong";
}

Of course write useful error messages with pertinent info that helps the user find their problem, and/or helps you fix your bug. You could get advanced and use something like set_error_handler(): http://php.net/manual/en/function.set-error-handler.php

I found this interesting article: http://www.derekallard.com/blog/post/error-handling-in-codeigniter/

I'm not sure it reflects the current CI release as it's from 2007.

0

精彩评论

暂无评论...
验证码 换一张
取 消