1. laravel中间件参数传递

1.1. 创建中间件 CheckApiLogin.php

php artisan make:middleware CheckApiLogin
<?php

namespace App\Http\Middleware;
use App\ApiModels\MembersModel;
use Closure;
use Illuminate\Support\Facades\Log;
class CheckApiLogin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        try{
            Functions::SysLog('Middleware_CheckApiLogin','==Request==',['data'=>$request]);
            $userid  = $request->input('userid',0);
            $apikey  = $request->input('apikey',0);
            if(empty($userid))
            {
                return response()->json(Functions::getMessageBody('用户id为空',[],1030));
            }
            if(empty($apikey))
            {
                return response()->json(Functions::getMessageBody('Apikey为空',[],1031));
            }
            list($status,$info) = $this->check_apikey($userid,$apikey);
            if($status){
                if(isset($info['userid']) && !empty($info['userid'])){
                    $_private_info = array();
                    $_private_info['userid']    = $userid;
                    $_private_info['user_info'] = $info;
                    //dump($_private_info);
                    #传递参数 $_private_info
                    $request->attributes->add(compact('_private_info'));
                    return $next($request);
                }else{
                    return response()->json(Functions::getMessageBody('网络繁忙哦',[],1029));
                }
            }
            return response()->json($info);
        }catch (\Exception $e){
            Functions::SysLog('Middleware_CheckApiLogin','==Exception==',['err'=>$e]);
            return response()->json(Functions::getMessageBody('系统繁忙,请稍后再试哦',[],20000));
        }
    }
    public function check_apikey($userid,$apikey)
    {
        /*****/
    }

}

1.2. 在中间件处理的方法中获取传递值

# Notice :构造方法无效
$_private_info = $this->requests->get('_private_info');

results matching ""

    No results matching ""