扩展Codeigniter异常类以添加自定义方法 [英] Extend Codeigniter Exceptions class to add a custom method
问题描述
我创建了一个新方法来处理401 Apache错误。
I created a new method to handle with 401 apache error.
我的核心类扩展了CI核心类,但是当我调用方法名称时,我收到以下消息: / p>
My core class extends CI core class but when I call the method name I receive this message:
致命错误:在
$ b中调用未定义的函数show_401()
$ bG:\Path\application\controllers\loader.php
第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;
}
}
推荐答案
您已将Exceptions核心类扩展了。
You have extended the Exceptions core class right.
只需确保已设置 subclass_prefix
配置如下:
Just make sure that you've set the subclass_prefix
config as follows:
$config['subclass_prefix'] = 'MI_';
但是似乎您使用了 show_401()
直接在Controller中运行函数。
However it seems you have used the show_401()
function directly within your Controller.
要点是,您不能将类方法作为函数在 global中访问作用域。
换句话说,您必须创建该类的一个实例以访问 show_401 ()
方法。
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.
这是因为CodeIgniter已创建 该方法的通用功能 ,该功能可帮助用户使用 Exception :: show_404()
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;
}
该函数使用另一个名为 load_class()的通用函数
获取Exceptions核心类的实例(以引用方式返回)以访问 show_404()
方法。
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.
公用功能与辅助功能相同,但在应用程序的最开始就加载了
。
获取 show_401()
函数可以工作
为了在全局范围内使用此函数,您可以创建一个辅助函数来创建一个实例
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.
CodeIgniter中没有内置的 Exception 辅助文件。因此,您可以在 application / helpers /
文件夹内创建 exceptions_helper.php
文件,以添加您的辅助函数:
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();
您还可以通过添加帮助程序来自动加载帮助程序文件(如果需要始终加载)将名称命名为 autoload.php
配置文件:
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'*/);
这篇关于扩展Codeigniter异常类以添加自定义方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!