I created a new method to handle with 401 apache error.
My core class extends CI core class but when I call the method name I receive this message:
Fatal error: Call to undefined function
show_401()
in
G:\Path\application\controllers\loader.php
on line 29
class MI_Exceptions extends CI_Exceptions {
function __construct()
{
parent::__construct();
}
/**
* 401 Unauthorized
*
* @access private
* @param string the page
* @param bool log error yes/no
* @return string
*/
function show_401($page = '', $log_error = TRUE)
{
$heading = "401 Unauthorized";
$message = "Unauthorized.";
// By default we log this, but allow a dev to skip it
if ($log_error)
{
log_message('error', '401 Unauthorized --> '.$page);
}
echo $this->show_error($heading, $message, 'error_401', 401);
exit;
}
}
You have extended the Exceptions core class right.
Just make sure that you've set the
subclass_prefix
config as follows:However it seems you have used the
show_401()
function directly within your Controller.The point is that you can't access the class methods as a function in global scope.
In other words, you have to create an instance of the class to access the
show_401()
method.But how does
show_404()
built-in function work?That's because CodeIgniter has created a Common function for that method which helps the users to use the
Exception::show_404()
method in global scope.system/core/Common.php
The function uses another common function named
load_class()
to get the instance of the Exceptions core class (returning by reference) in order to access theshow_404()
method.Getting
show_401()
function to workIn order to use this function in global scope, you could create a helper function to create an instance of the class, and access you're custom method.
There's no Exceptions built-in helper file in CodeIgniter. Thus, you could create
exceptions_helper.php
file insideapplication/helpers/
folder to add your helper function:application/helpers/exceptions_helper.php
Finally, load the helper file within your Controller:
You could also load the helper file automatically (if it's necessary to be loaded always) by adding the helper name into the
autoload.php
config file: