简体   繁体   中英

Trouble with Authenticate Middleware in Laravel 5.1

I need help with a problem that I cannot solve by myself. I'm using Laravel 5.1 and when I try to enable the Authenticate Middleware I receive this error.

    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'

I have the middleware as it comes by default with Laravel, also the kernel.php, both look like this

    <?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);
    }

}

And the kernel:

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

And I use it from here:

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

I dont know what could be happening at all. Thanks for reading

I think you are mixing up everything you found regarding middlewares.

  1. Why calling $this->middleware('auth', ['only' => 'admin']); on your constructor? Have a read here
  2. Your handle method signature is : public function handle($request, Closure $next). You are passing an array as well?
  3. How do you mange your users roles?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM