中介軟體與控制器或路由傳值
阿新 • • 發佈:2019-01-06
<?php
namespace App\Http\Middleware;
use Closure;use Session;
class CheckToken{ /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @returnmixed */ public function handle($request, Closure $next) { Session::put('userinfo.id' ,1); Session::put('userinfo.truename','測試'); Session::put('userinfo.uname','DJA888999'); Session::put('userinfo.identify','DJA'); Session::put('userinfo.gradetime','永久'); $userinfo = Session('userinfo'); $uid = $userinfo['id']; if(empty($uid)){ return redirect('login'); } $request->attributes->add(['userinfo'=>$userinfo]); // $request->attributes->add(compact('userinfo')); //直接以把userinfo作為key其$userinfo值作為值放入$request // $request->merge(['userinfo'=>$userinfo]); // 能使用Request::input方法獲取 return $next($request); }}
namespace App\Http\Middleware;
use Closure;use Session;
class CheckToken{ /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @returnmixed */ public function handle($request, Closure $next) { Session::put('userinfo.id'