Home  >  Article  >  Backend Development  >  The use of behaviors in ThinkPHP5

The use of behaviors in ThinkPHP5

不言
不言Original
2018-04-04 14:47:541716browse

本篇文章中的内容是ThinkPHP5中行为的使用 ,现在分享给大家,有需要的朋友可以参考一下这篇文章的内容

目录结构:
The use of behaviors in ThinkPHP5
1.在user\behavior目录下建UserCheck.php(名称随便取)

<?phpnamespace app\user\behavior;use think\Controller;/**
* 
*/class UserCheck {
    use \traits\controller\Jump;//类里面引入jump;类

    //绑定到CheckAuth标签,可以用于检测Session以用来判断用户是否登录
    public function run(&$params){

        return $this->error(&#39;请登录!&#39;,&#39;index/login&#39;);
    }
}
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

  • 15

  • 16

  • 17

  • 18

这里run函数中添加自己检测用户权限的逻辑,可以使用session或者别的…
2.在application目录下tags.php(没有就新建一个,名字固定不能改)把行为与某个标签绑定,这里绑定到了“CheckAuth”

<?phpreturn [    &#39;CheckAuth&#39; => [        &#39;app\\user\\behavior\\UserCheck&#39;,
    ],

];
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

3 . 在控制器里需要监听的地方写下监听标签代码
user\controller\Index.php

<?phpnamespace app\user\controller;use think\Hook;class Index{
    public function index(){

  //判断用户是否登录
    Hook::listen(&#39;CheckAuth&#39;,$params);        //
        echo &#39;index_end&#39;;
    }


}


转载:http://blog.csdn.net/u012995856

相关推荐:  

thinkPHP5框架的分页查询功能实现方法 

            

The above is the detailed content of The use of behaviors in ThinkPHP5. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn