Codeigniter Library for managing error messages generated by system (not PHP errors)
Author: Michael M Chiwere
- Copy and merge contents of application folder into your codeigniter application folder
- Open the Error.php library to setup some default variables for your error messages
- Open autoload.php in application/config/ folder and add the 'error' in the libraries array. Make sure the 'session' library is also auto loaded and must be loaded before the 'error' library.
- Also add 'error' and 'url' to the helpers array in the autoload.php file
- If you are using bootstrap 3 and above, then thats all.
- If you are not using bootstrap, you may need to setup your own css classes to format the error messages
- Set error messages by using the sem() method. see examples below
- display error messages by calling the gem() method
1a. suppose an error has occured e.g during login, username/password are incorrect, set error message as follows:
method A: sem('Login Failed, please check your username/password');
method B: sem('Login Failed, please check your username/password', TRUE);
method C: sem( [ 'error'=>TRUE, 'error_msg'=>'Login Failed, please check your username/password' ] );
1b. suppose an operation was successful, e.g account created successfully. Set success message as follows:
method A: sem('Thank you for registering. You account has been created successfully', FALSE);
method B: sem( ['error'=>FALSE, 'error_msg'=>'Thank you for registering. You account has been created successfully'] );
1c. suppose you also want the error message to be written to the log file. Note that you can send the log flag as either 'ERROR' or 'DEBUG'
method A: sem('Login Failed, please check your username/password', TRUE, 'ERROR');
method B: sem( [ 'error'=>TRUE, 'error_msg'=>'Login Failed, please check your username/password' ], TRUE, 'ERROR' );
2a. gem() function will return an array containing the error messages and the error flags (e.g success, error, info).
// php code to loop through array of error messages
$error = gem();
<?php foreach( $error as $e ):?>
<div class="<?php echo $e['f'];?>">
<?php echo $e['msg'];?>
</div>
<?php endforeach;?>
3a. show_errors() function will print the error messages at the exact place you call it
3b. show_errors(TRUE) function will also display form validation errors if you are using the codeigniter form_validation library.
3c. show_errors( FALSE, TRUE, 'custom_message_field' ) will also fetch a string from flashdata field with the name you specify in the third parameter.
Need help, contact me on michaelmartinc@gmail.com
THANK YOU!