簡體   English   中英

Laravel 5.3中間件(Auth :: user())= null

[英]Laravel 5.3 middleware (Auth::user()) = null

我用來檢查用戶是否是具有中間件的管理員,但無法使用5.3。 我以前使用過此代碼:

if(Auth::guest()) //If the user is just a guest
    {
        return redirect("/");
    }
    if(Auth::user()->isAdmin == 1) //If the user is logged in and he is the Admin
    {
        return $next($request);
    }
    else //If the user is not the admin he can't see this page
    {
        return redirect("/");
    }
}

目前,我沒有運氣就嘗試過以下方法。 誰能向我指出我們如何檢查用戶是管理員還是普通用戶的正確方向?

    /**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @return mixed
 */
public function handle($request, Closure $next)
{

    if ($request){
        if ($request->user()->role == 'Admin'){
            return "yes";
        }
    }

    return $next($request);
}

您只有一個= 您必須使用== (或更嚴格地使用=== )。

例如,“ =”運算符將值分配給變量。 但是,“ ==”將某些內容與另一個進行比較。

在這段代碼中:

if ($request){
    if ($request->user()->role = 'Admin'){
        return "yes";
    }
}

您正在訪問“ $ request”對象並從中拉出用戶角色。 您打算使用字符串'Admin'評估返回的'$ request'對象,但是,您不是在評估而是嘗試對其進行分配。

要解決此問題:

if ($request){
    if ($request->user()->role == 'Admin'){
        return "yes";
    }
}

使用“ ==”運算符將給定值與您提供的靜態字符串進行比較。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM