简体   繁体   English

Ion Auth检查从MY_L​​oader类登录

[英]Ion Auth check login from MY_Loader class

This may be a basic Codeigniter (or php) question, but I'm trying to check if a user is logged in with Ion Auth for every page call so I can provide a 'Sign-in' or 'Sign-out' link. 这可能是一个基本的Codeigniter(或php)问题,但我正在尝试检查用户是否使用Ion Auth登录每个页面调用,以便我可以提供“登录”或“注销”链接。

I have my header, content, and footer views loading from MY_Loader class and was trying to use $this->ion_auth->logged_in() to check if a user is logged in, but I get this php error, 我从MY_L​​oader类加载我的标题,内容和页脚视图,并尝试使用$this->ion_auth->logged_in()来检查用户是否已登录,但是我收到此php错误,

PHP Error: Undefined property: MY_Loader::$ion_auth PHP错误:未定义属性:MY_Loader :: $ ion_auth

Here is MY_Loader.php: 这是MY_Loader.php:

<?php

/**
 * /application/core/MY_Loader.php
 *
 */
class MY_Loader extends CI_Loader {
    public function template_main($template_name, $vars = array(), $return = FALSE)
    {
        $vars['signin'] = $this->ion_auth->logged_in() ? 'signout' : 'signin';
        $vars['signin_text'] = $this->ion_auth->logged_in() ? 'Sign-out' : 'Sign-in';
        $content  = $this->view('frontend/main_template/header', $vars, $return);
        $content .= $this->view($template_name, $vars, $return);
        $content .= $this->view('frontend/main_template/footer', $vars, $return);

        if ($return)
        {
            return $content;
        }
    }
}

/* End of MY_Loader.php */
/* Location: ./application/core/MY_Loader.php */

I am autoloading the ion_auth library. 我正在自动加载ion_auth库。 How can I call the logged-in() function in MY_Loader class? 如何调用MY_Loader类中的logged-in()函数?

The autoloaded files probably haven't been loaded yet. 自动加载的文件可能尚未加载。 Add $this->load->library('ion_auth') to the top of the method. 将$ this-> load-> library('ion_auth')添加到方法的顶部。

EDIT: I misread the question, so sorry. 编辑:我误解了这个问题,很抱歉。 But I'm leaving the answer up for future readers that might find it useful. 但是我将为未来的读者留下答案,这些读者可能会觉得它很有用。 Try using a hook to solve your problem. 尝试使用钩子来解决您的问题。

My suggestion is to simply check if the user is logged in from your controller's constructor method and save the result to a class variable. 我的建议是简单地检查用户是否从控制器的构造方法登录并将结果保存到类变量中。

$this->user_logged_in = (bool)$this->ion_auth->logged_in();

This would let you make the call once for each controller, instead of once for each view (easier to maintain, and keeping basic ACL/logic out of the view). 这将允许您为每个控制器调用一次,而不是每次调用一次(更容易维护,并保持基本ACL /逻辑不在视图中)。 Then all you need to do is pass the variable to your view. 然后,您需要做的就是将变量传递给您的视图。

$data['user_logged_in'] = $this->user_logged_in;
$this->load->view('view_file_name', $data);

What I like to do when I want the entire app to require a login is to call these types of checks via CodeIgniter's hooks . 当我希望整个应用程序需要登录时,我喜欢做的是通过CodeIgniter的钩子调用这些类型的检查。 In this case, maybe the post_controller_constructor hook, this way it's called automatically (just make sure to filter out the pages you want to ignore this check, ie. login & register, etc). 在这种情况下,也许是post_controller_constructor挂钩,这样它就会被自动调用(只需确保过滤掉你想忽略这个检查的页面,即登录和注册等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM