0

I want to let only authenticated users access a certain page in my application. After reading the documentation I found out this can be done using middleware. I assigned the auth middleware to the route which I want to protect, but I can still access it even if I am not authenticated.

Route::get("add","Clubs@add",['middleware' => 'auth', function () {}]);
1

1 Answer 1

7

I think you're looking for this syntax.

Route::get('profile', [
    'middleware' => 'auth',
    'uses' => 'ProfileController@show'
]);

So for your case, it should look like this.

Route::get('add', [
    'middleware' => 'auth',
    'uses' => 'Clubs@add'
]);
3
  • This still doesn't work. I managed to solve the problem by calling the middleware from the constructor of the controller. But it should work even if I assign it in the routes.
    – Borut Flis
    Jun 18, 2016 at 23:20
  • Yes, that should work. What version of Laravel are you running?
    – hobenkr
    Jun 18, 2016 at 23:21
  • I can't think of any reason the syntax I posted would not work. This is probably a silly question because it seems like you know what you're doing, but did you replace the route path and controller you use in the example I posted so it looks like the second example I just added?
    – hobenkr
    Jun 19, 2016 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.