dongxu4023 2014-02-12 17:58
浏览 74
已采纳

扩展Codeigniter Exceptions类以添加自定义方法

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条回答 默认 最新

  • douliang2167 2014-02-12 20:35
    关注

    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'*/);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!