Apply middlewrae to a LARAVEL route

0

I would like to know how I can apply a middleware to a specific route in Laravel 5.6 , what I intend is that only logged-in users can access the route.

Route::get('https://josearandav.github.io/Tea-Docs/docs/', function () {
    return view();
})->middleware('auth');

Access to this route is in a button of a menu. Thanks for your help masters!

    
asked by Ronny 19.06.2018 в 01:39
source

1 answer

0

I recommend that you do it as follows:

  • Create the route as follows, pointing to a method within a controller

    Route::get('/admin', 'AdminController@login');

  • Later in a so-called method called login within the Controller AdminController, do the following

  • declares the following method

    public function __construct() 
    {
      $this->middleware('auth');
    }
    

    And later declares the login method

    public function login()
    {
       return view('login');
    }
    

    Since we already want to protect the route, we do it as follows

    Route::get('/admin', 'AdminController@login')->middleware('auth');
    
        
    answered by 19.06.2018 в 01:51