Support the ongoing development of Laravel.io →
posted 10 years ago
Requests
Last updated 2 years ago.
0
Route::post('register', 'LoginController@login');
Route::post('register', 'LoginController@logout')->before('auth');

This will work, but the issue is that you're trying to assign the register POST route to two different controller methods, and in this case it will use the last one @logout. Note though, you can assign the same route name using a different HTTP method (GET & POST).

I'd recommend changing the name of the controller though, to something like AuthController, since it does more than login.

Also I'm confused as to why you're doing a POST request on a logout route? Really POST requests are used when sending data via forms. A simple logout route would usually look like:

# Route
Route::get('logout', array('as' => 'logout', 'before' => 'auth', 'uses' => 'AuthController@getLogout'));

# AuthController Method
public function getLogout()
{
   Auth::logout();

   return Redirect::to('/');
});

I'd imagine you'd want something like:

GET `login` = Returns your login View, with the forms etc
POST `login` = Accepts a post request from the form in the View (from GET `login`)
GET `register` = Shows a view with the register details
POST `register` = Accepts a post request from the form in the View (from GET `register`)
GET `logout` = As above code, logs the user out and redirects them.
Last updated 2 years ago.
0

Thanks for the help and the detailed answer :) One note: My Login-Form is part of the header and therefore always present, so I don't really need a login view.

So I can just define a route::post for "login" to make it work, but then if someone tries to access the login route via GET, he will get a MethodNotAllowedHttpException. Sooo should I just handle this exception and perform a redirect or is there a different way to handle the login without a new route?

Last updated 2 years ago.
0

You should give a different route name for each action. Why would you name 'register' a route that point to the login action ?

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.