本篇文章中的内容是ThinkPHP5中行为的使用 ,现在分享给大家,有需要的朋友可以参考一下这篇文章的内容
目录结构:
1.在userehavior目录下建usercheck.php(名称随便取)
<?phpnamespace appuserehavior;use thinkController;/** * */class UserCheck { use raitscontrollerJump;//类里面引入jump;类 //绑定到CheckAuth标签,可以用于检测Session以用来判断用户是否登录 public function run(&$params){ return $this->error('请登录!','index/login'); } }
1
2
3
4
立即学习“PHP免费学习笔记(深入)”;
5
6
7
8
9
10
11
12
13
14
15
16
17
18
这里run函数中添加自己检测用户权限的逻辑,可以使用session或者别的…
2.在application目录下tags.php(没有就新建一个,名字固定不能改)把行为与某个标签绑定,这里绑定到了“CheckAuth”
<?phpreturn [ 'CheckAuth' => [ 'app\user\behavior\UserCheck', ], ];
1
2
3
4
立即学习“PHP免费学习笔记(深入)”;
5
6
7
3 . 在控制器里需要监听的地方写下监听标签代码
usercontrollerIndex.php
<?phpnamespace appusercontroller;use thinkHook;class Index{ public function index(){ //判断用户是否登录 Hook::listen('CheckAuth',$params); // echo 'index_end'; } } 转载:http://blog.csdn.net/u012995856
相关推荐:
以上就是ThinkPHP5中行为的使用 的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号