bossmeow
4 years ago
581
2
Laravel

Middleware Issue - Call to undefined method App\Http\Middleware\Authenticate::middleware()

Posted 4 years ago by bossmeow

Hi all,

I created another middleware for Admin and using it to restrict users from accessing the administrator pages. For normal users, I tried using Auth Middleware in route.php and I got the error as below.

"FatalErrorException in Authenticate.php line 27: Call to undefined method App\Http\Middleware\Authenticate::middleware()"

Route.php

Route::get('/', function () {return view('welcome'); });
    Route::get('index', function () {return view('index'); });
    Route::get('faq', function () {return view('faq'); });
    Route::get('terms', function () {return view('terms'); });
    Route::get('privacy', function () {return view('privacy'); });
    
    Route::get('contact', ['as' => 'contact', 'uses' => 'ContactController@create']);
    Route::post('contact', ['as' => 'contact_store', 'uses' => 'ContactController@store']);

    Route::get('auth/register', 'Auth\AuthController@getRegister');
    Route::post('auth/register', 'Auth\AuthController@postRegister');

    Route::get('auth/login', 'Auth\AuthController@getLogin');
    Route::post('auth/login', 'Auth\AuthController@postLogin');

    Route::get('auth/logout', 'Auth\AuthController@getLogout');


    Route::group(array('prefix' => 'admin', 'namespace' => 'Admin', 'middleware' => 'admin'), function() {
            
            Route::resource('user', 'UserController');
    
    });

        Route::group(array('prefix' => 'home', 'namespace' => 'Home', 'middleware' => 'auth'), function() {
            
            Route::resource('/', 'HomeController@index');
    
    });


Middleware\Authenticate.php

<?php
namespace App\Http\Middleware;

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


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->middleware('guest', ['except => getLogout']);
    }

    /**
     * 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');
            }
        }
        
        
    }
    
    
    protected $redirectTo = '/home';
    
    
}

Thanks in advance.

Please sign in or create an account to participate in this conversation.