Extend Codeigniter Exceptions class to add a custo

2019-07-15 09:39发布

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;
    }
}

1条回答
你好瞎i
2楼-- · 2019-07-15 10:01

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'*/);
查看更多
登录 后发表回答