code-8
code-8

Reputation: 58642

What is the best way to restrict any routes in Laravel 5.0?

I want to restrict some routes of my application and only allow that to only my authenticated user.

I tried check using the auth:check() function but it doesn't seem to work.

// Route Restriction
if (Auth::check()){

    //Web Directory
    Route::get('web-directory','WebDirectoryController@index');
}

When I got to mysite/web-directory I still get 404 Error - even if I'm currently log-in.

enter image description here

What is the best way to restrict any routes in Laravel 5.0 ?

Upvotes: 2

Views: 1229

Answers (2)

manix
manix

Reputation: 14747

This can be achieved by restricting routes individually too:

Route::get('web-directory', [
    'middleware' => 'auth', 
    'uses' => 'WebDirectoryController@index'
]);

Upvotes: 2

code-8
code-8

Reputation: 58642

All right, so I figured out the solution to my own question.

I restrict my routes by doing this

// Route group
$router->group(['middleware' => 'auth'], function() {

    //Web Directory
    Route::get('web-directory','WebDirectoryController@index');
}

Now, I can go to my route fine, and 404 Error will only kick in when the user is not yet log-in.

I hope this help someone.

Upvotes: 3

Related Questions