简体   繁体   English

检查用户是否已登录-使用Codeigniter HMVC进行离子验证

[英]Check if user is logged in - Ion Auth with Codeigniter HMVC

I've just switched from standard CI to Codeigniter Skeleton with HMVC. 我刚刚使用HMVC从标准CI切换到了Codeigniter Skeleton。 I'm trying to check if the user is logged in and display a menu item according to the status. 我正在尝试检查用户是否已登录并根据状态显示菜单项。

This code works on standard CI with ion_auth: 此代码可通过ion_auth在标准CI上运行:

<ul class="nav navbar-nav pull-right">
<!-- User Tab -->
<?php if (!$this->ion_auth->logged_in()): ?>
    <li><a href="<?php echo site_url('auth/login'); ?>">Log in</a></li>
<?php else: ?> 
    <li class="dropdown">
        <a class="dropdown-toggle" data-toggle="dropdown" href="#">
        My Account <b class="caret"></b>
        </a>
        <ul class="dropdown-menu">
            <li><a href="<?php echo site_url('auth/logout'); ?>">Logout</a></li>
       </ul>
    </li>
<?php endif ?>
</ul>

If I use this with HMVC and ion_auth, I get the following error: 如果将其与HMVC和ion_auth一起使用,则会出现以下错误:

A PHP Error was encountered 遇到PHP错误

Severity: Notice 严重程度:注意

Message: Undefined property: CI::$ion_auth 消息:未定义的属性:CI :: $ ion_auth

Filename: MX/Loader.php 文件名:MX / Loader.php

Line Number: 279 行号:279

Fatal error: Call to a member function logged_in() on a non-object in C:\\wamp\\www\\myapp\\application\\views\\header.php on line 59 致命错误:在第59行上的C:\\ wamp \\ www \\ myapp \\ application \\ views \\ header.php中的非对象上调用成员函数logging_in()

I've tried adding the ion_auth library to the autoload.php, but then I get a "Cannot redeclare class Ion_auth" error when clicking on the link or using any of the ion_auth routes. 我尝试将ion_auth库添加到autoload.php中,但是单击链接或使用任何ion_auth路由时,出现“无法重新声明类Ion_auth”错误。

I solved this by removing the following line from application/modules/auth/controllers/auth.php 我通过从application / modules / auth / controllers / auth.php中删除以下行来解决此问题

$this->load->library('authentication', NULL, 'ion_auth'); 

And adding 'ion_auth' to the autoload.php 并在autoload.php中添加“ ion_auth”

I can now use the ion_auth functions in both controllers and view and the default auth routes like /auth/login/ still work as expected. 我现在可以在两个控制器和视图中使用ion_auth函数,并且默认的auth路由(如/ auth / login /)仍可以按预期工作。

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

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