CodeIgniter Basic Tutorial
Performance & Utilities
CodeIgniter Advanced
CodeIgniter provides a set of common functions that are globally available across your application.
These functions make coding faster, easier, and more consistent since you don’t need to re-write basic PHP logic.
Examples include:
They are defined inside:
system/core/Common.php
And are always loaded automatically by CodeIgniter.
show_error()
Displays a custom error message.
show_error("Something went wrong!", 500, "Error Title");
show_404()
Shows a 404 page when a resource is not found.
show_404();
log_message()
Used for logging errors, debugging, or info messages.
log_message('error', 'An error occurred'); log_message('debug', 'Debugging info here'); log_message('info', 'Just some info');
Logs are stored in:
application/logs/
get_mimes()
Returns the MIME types defined in application/config/mimes.php
.
print_r(get_mimes());
config_item()
Retrieves a value from the configuration file.
$site_name = config_item('base_url'); echo $site_name;
is_php()
Checks the running PHP version.
if (is_php('7.4')) { echo "Running PHP 7.4 or higher!"; }
is_loaded()
Checks if a certain class is already loaded.
print_r(is_loaded());
set_status_header()
Sets the HTTP status code in response.
set_status_header(200); // OK set_status_header(403); // Forbidden
class CommonDemo extends CI_Controller { public function index() { // Logging log_message('info', 'User visited CommonDemo controller.'); // Error example if (!file_exists('somefile.txt')) { show_error('The requested file was not found.', 404); } // Config value echo "Base URL is: " . config_item('base_url'); } }