簡體   English   中英

Laravel 5.1中的身份驗證中間件出現問題

[英]Trouble with Authenticate Middleware in Laravel 5.1

對於我自己無法解決的問題,我需要幫助。 我正在使用Laravel 5.1,當我嘗試啟用身份驗證中間件時,收到此錯誤。

    ErrorException in Manager.php line 137:
call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Auth\Guard' does not have a method 'handle'

我有Laravel默認提供的中間件,還有kernel.php,兩者都看起來像這樣

    <?php

namespace Imuva\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Guard;

class Authenticate {

    /**
     * The Guard implementation.
     *
     * @var Guard
     */
    protected $auth;

    /**
     * Create a new filter instance.
     *
     * @param  Guard  $auth
     * @return void
     */
    public function __construct(Guard $auth) {
        $this->auth = $auth;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next) {
        if ($this->auth->guest()) {
            if ($request->ajax()) {
                return response('Unauthorized.', 401);
            } else {
                return redirect()->guest('auth/login');
            }
        }

        return $next($request);
    }

}

和內核:

protected $routeMiddleware = [
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'guest' => \Imuva\Http\Middleware\RedirectIfAuthenticated::class,
        'auth' => \Imuva\Http\Middleware\Authenticate::class,
    ];

我從這里使用它:

class HomeController extends Controller {
public function __construct() {
   $this->middleware('auth', ['only' => 'admin']);
}

我完全不知道會發生什么。 謝謝閱讀

我認為您正在混淆關於中間件的所有發現。

  1. 為什么調用$ this-> middleware('auth',['only'=>'admin']); 在你的構造函數上? 在這里閱讀
  2. 您的handle方法簽名是:公共函數handle($ request,Closure $ next)。 您也要傳遞數組嗎?
  3. 您如何管理用戶角色?

暫無
暫無

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

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