创建 Codeigniter 助手 ->未定义的方法

发布于 2024-12-28 05:02:23 字数 1029 浏览 2 评论 0原文

中创建了一个助手“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海螺姑娘 2025-01-04 05:02:23

“is_login”是一个函数,而不是一个方法。只需将 $this->is_login(); 替换为 is_login(); 即可。

"is_login" is a function, not a method. Just replace $this->is_login(); with is_login();.

℡寂寞咖啡 2025-01-04 05:02:23

助手不是方法,它们只是简单的函数调用。

查看用户指南中的帮助程序: http://codeigniter.com/user_guide/general/helpers .html

加载助手:

$this->load->helper('url');

使用其助手函数:

<?php echo anchor('blog/comments', 'Click Here');?>

其中 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:

$this->load->helper('url');

using its helper functions:

<?php echo anchor('blog/comments', 'Click Here');?>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文