【问题标题】:Trouble with Authenticate Middleware in Laravel 5.1Laravel 5.1 中身份验证中间件的问题
【发布时间】:2016-06-13 19:45:42
【问题描述】:

我需要帮助解决我自己无法解决的问题。 我正在使用 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']);
}

我根本不知道会发生什么。感谢阅读

【问题讨论】:

  • 似乎是保护对象被初始化而不是中间件。你能粘贴你的 Imua\Http\Middleware\Authenticate 类吗?
  • 我已将其添加到主消息 Authenticate 但如果我没记错的话我没有从默认安装中触及它。

标签: php laravel middleware


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-03
    • 2016-12-30
    • 1970-01-01
    • 2019-03-15
    • 2016-06-11
    • 1970-01-01
    • 2014-05-10
    相关资源
    最近更新 更多