PHP code example of hainuo / think-rbac

1. Go to this page and download the library: Download hainuo/think-rbac library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hainuo / think-rbac example snippets


        namespace app\admin\controller;
        class Auth extends AdminBase
        {
            public function _empty($name)
            {
                $auth = new \think\auth\Auth();
                $auth = $auth->autoload($name);
                if ($auth) {
                    if (isset($auth['code'])) {
                        return json($auth);
                    } elseif (isset($auth['file'])) {
                        return $auth['file'];
                    }
                    $this->view->engine->layout(false);
                    return $this->fetch($auth[0], $auth[1]);
                }
                return abort(404, '页面不存在');
            }
        }
        

        return [
            //项目配置
            'rbac' => [
                'session_prefix'  => 'rbac_', //rbac缓存数据的前缀
                'supper_id'       => 28,//超级管理员ID
                'style_directory' => null, //静态文件目录  默认不设置
            ],
        ];
        

       public function _initialize()
          {
              parent::_initialize(); // TODO: Change the autogenerated stub
              session('user',['uid'=>1]);
          }
      

      public function __construct()
      {
          parent::__construct();
          $auth                   = new Auth();
          $auth->noNeedCheckRules = ['index/index/index','index/index/home'];
          $auth->log              = true;                 // v1.1版本  日志开关默认true
          $user                   = $auth::is_login();

          if($user){//用户登录状态
              $this->uid = $user['uid'];
              if(!$auth->auth()){
                  return $this->error("你没有权限访问!");
              }
          }else{
              return $this->error("您还没有登录!",url("publics/login"));
          }
      }
      
shell
         ├── admin
         │   ├── controller
         │   │   ├── AdminBase.php
         │   │   ├── Auth.php
         │   ├── view   
         |   └── config.php