PHP code example of yytwow / think-auth

1. Go to this page and download the library: Download yytwow/think-auth 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/ */

    

yytwow / think-auth example snippets


php think auth:config 

php think make:middleware Auth


namespace app\http\middleware;

use think\exception\HttpResponseException;
use think\Auth AS AuthHandle;

class Auth
{

    /**
     * 授权业务处理
     * @param $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle($request, \Closure $next)
    {
        // 白名单
        $allow = ['user/login'];

        $rule = strtolower("{$request->controller()}/{$request->action()}");
        
        // 初始化 user_id
        $user_id = is_login();

        // 权限检查
        $check = AuthHandle::check($rule, $user_id);
        if (false === $check) {
            $this->error('[403] 未授权访问');
        }

        return $next($request);
    }

}

$auth->check('rule1,rule2',uid);