取得經(jīng)過認證的用戶

2018-02-24 15:51 更新

當(dāng)用戶通過認證后,有幾種方式取得用戶實例。

首先, 你可以從 Auth facade 取得用戶:

<?php namespace App\Http\Controllers;

use Illuminate\Routing\Controller;

class ProfileController extends Controller {

    /**
     * Update the user's profile.
     *
     * @return Response
     */
    public function updateProfile()
    {
        if (Auth::user())
        {
            // Auth::user() returns an instance of the authenticated user...
        }
    }

}

第二種,你可以使用 Illuminate\Http\Request 實例取得認證過的用戶:

<?php namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

class ProfileController extends Controller {

    /**
     * Update the user's profile.
     *
     * @return Response
     */
    public function updateProfile(Request $request)
    {
        if ($request->user())
        {
            // $request->user() returns an instance of the authenticated user...
        }
    }

}

第三,你可以使用 Illuminate\Contracts\Auth\Authenticatable contract 類型提示。這個類型提示可以用在控制器的構(gòu)造方法,控制器的其他方法,或是其他可以通過服務(wù)容器 解析的類的構(gòu)造方法:

<?php namespace App\Http\Controllers;

use Illuminate\Routing\Controller;
use Illuminate\Contracts\Auth\Authenticatable;

class ProfileController extends Controller {

    /**
     * Update the user's profile.
     *
     * @return Response
     */
    public function updateProfile(Authenticatable $user)
    {
        // $user is an instance of the authenticated user...
    }

}
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號