Vishal Desai
Vishal Desai

Reputation: 189

advanced authentication routes in laravel 5

i currently have /dashboard route that is behind login page, which after login takes you to admin panel.

Route::get('dashboard', ['middleware' => 'auth', function()
{
    return view('dash.dashboard');
}]);

this is working fine. But I can't figure out how to prevent access to all the links in admin panel if not logged in. how can i prevent all the dashboard/{} routes? note - I'm still learning laravel.

Upvotes: 0

Views: 450

Answers (2)

sulavvr
sulavvr

Reputation: 276

What you can do is, you can create a controller, let's say DashboardController and create a route, like the following:

$router->get('dashboard', 'DashboardController@index');
// calling the index function of your DashboardController

And in your DashboardController, do this in your constructor:

class DashboardController extends BaseController
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        return view('dash.dashboard');
    }
}

Create other functions in this controller, so everytime, the functions in this controller are called, it will check the auth middleware. This is a good way to do it.

The other way to do it is to use route group.

 $router->group(['middleware' => 'auth'], function() {
     $router->get('dashboard', function(){
         return view('dash.dashboard');
     });
 });

Upvotes: 1

jarguez01
jarguez01

Reputation: 194

You can use Route::group() to wrap all your admin routes and make them use the auth middleware, example :

Route::group(['middleware' => ['foo', 'bar']], function()
{
    Route::get('/', function()
    {
        // Has Foo And Bar Middleware
    });

    Route::get('user/profile', function()
    {
        // Has Foo And Bar Middleware
    });

});

See more in the documentation

Upvotes: 2

Related Questions