在Laravel 5.1中验证中间件的麻烦

时间:2016-06-10 09:35:21

标签: php laravel middleware

我需要帮助解决一个我自己无法解决的问题。 我正在使用Laravel 5.1,当我尝试启用Authenticate Middleware时,我收到此错误。

    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 个答案:

答案 0 :(得分:0)

我认为你混淆了你发现的有关中间件的所有内容。

  1. 为什么要拨打$ this-&gt;中间件('auth',['only'=&gt;'admin']);在你的构造函数?阅读here
  2. 您的句柄方法签名是:public function handle($ request,Closure $ next)。你也传递一个数组?
  3. 您如何管理用户角色?