ThinkPHP5开发(三)使用Behavior检测用户登录状态
行为behavior参考了:http://www.thinkphp.cn/topic/38941.html
不继承Controller使用error参考了:http://www.thinkphp.cn/topic/40137.html
目录结构:
1.在userehavior目录下建UserCheck.php(名称随便取)
<?php
namespace appuserehavior;
use thinkController;
/**
*
*/
class UserCheck
{
use raitscontrollerJump;//类里面引入jump;类
//绑定到CheckAuth标签,可以用于检测Session以用来判断用户是否登录
public function run(&$params){
return $this->error("请登录!","index/login");
}
}
这里run函数中添加自己检测用户权限的逻辑,可以使用session或者别的…
2.在application目录下tags.php(没有就新建一个,名字固定不能改)把行为与某个标签绑定,这里绑定到了“CheckAuth”
<?php
return [
"CheckAuth" => [
"app\user\behavior\UserCheck",
],
];
3 . 在控制器里需要监听的地方写下监听标签代码
usercontrollerIndex.php
<?php
namespace appusercontroller;
use thinkHook;
class Index{
public function index(){
//判断用户是否登录
Hook::listen("CheckAuth",$params);
//
echo "index_end";
}
}
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇: ThinkPHP5开发(四)查询用户列表
- 下一篇:没有了
