You have extended the Exceptions core class right.
Just make sure that you've set the subclass_prefix
config as follows:
$config['subclass_prefix'] = 'MI_';
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
function show_404($page = '', $log_error = TRUE)
{
$_error =& load_class('Exceptions', 'core');
$_error->show_404($page, $log_error);
exit;
}
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 the show_404()
method.
Common functions as the same as helper functions, but they're loaded
at very beginning of the application.
Getting show_401()
function to work
In 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 inside application/helpers/
folder to add your helper function:
application/helpers/exceptions_helper.php
if ( ! function_exists('show_401'))
{
function show_401($page = '', $log_error = TRUE)
{
$_error =& load_class('Exceptions', 'core');
$_error->show_401($page, $log_error);
exit;
}
}
Finally, load the helper file within your Controller:
$this->load->helper('exceptions');
// Then use the function
show_401();
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:
$autoload['helper'] = array('exceptions'/*, 'language', 'form'*/);