创建 Codeigniter 助手 ->未定义的方法
中创建了一个助手“session_helper.php”
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('is_login'))
{
function is_login()
{
$CI =& get_instance();
$is_logged_in = $CI->session->userdata('is_logged_in');
if (!isset($is_logged_in) || $is_logged_in != TRUE) {
redirect('login');
}
}
}
我在 application/helpers/ 文件夹和“Configuracion”控制器
class Configuracion extends CI_Controller {
function __construct()
{
parent::__construct();
$this->is_logged_in();
}
function is_logged_in()
{
$this->load->helper('session');
$this->is_login();
}
}
:问题是当我调用控制器“http://localhost/proyect/configuracion”时出现以下错误:
致命错误:调用到第 15 行 C:...\application\controllers\configuracion.php 中未定义的方法 Configuracion::is_login()
我阅读了手册,显然一切都是正确的...出了什么问题?
I created a helper "session_helper.php" in application/helpers/ folder
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('is_login'))
{
function is_login()
{
$CI =& get_instance();
$is_logged_in = $CI->session->userdata('is_logged_in');
if (!isset($is_logged_in) || $is_logged_in != TRUE) {
redirect('login');
}
}
}
And "Configuracion" Controller:
class Configuracion extends CI_Controller {
function __construct()
{
parent::__construct();
$this->is_logged_in();
}
function is_logged_in()
{
$this->load->helper('session');
$this->is_login();
}
}
The problem is when I call the controller "http://localhost/proyect/configuracion" I get the following error:
Fatal error: Call to undefined method Configuracion::is_login() in C:...\application\controllers\configuracion.php on line 15
I read the manual and apparently everything is correct ... what is wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“is_login”是一个函数,而不是一个方法。只需将
$this->is_login();
替换为is_login();
即可。"is_login" is a function, not a method. Just replace
$this->is_login();
withis_login();
.助手不是方法,它们只是简单的函数调用。
查看用户指南中的帮助程序: http://codeigniter.com/user_guide/general/helpers .html
加载助手:
使用其助手函数:
其中
anchor()
是已加载助手的一部分的函数。另外,我敦促您不要将助手称为“会话”,使其更具描述性,因为稍后可能会变得混乱。只是一个建议,完全取决于你。
Helpers are not methods, they are just simple function calls.
Take a look at helpers in the User Guide: http://codeigniter.com/user_guide/general/helpers.html
Loading a helper:
using its helper functions:
where
anchor()
is the function that is part of the loaded helper.Also I would urge you to stay way from calling a helper 'session' make it something more descriptive, as it might become confusing later on. Just a suggestion, its entirely up to you.