Help with middleware group

Asked

Viewed 31 times

0

My web.php file looks like this:

Route::get('/login', 'AutenticacaoController@login')->name('autenticacao.login');
Route::post('/logar', 'AutenticacaoController@logar')->name('autenticacao.logar');
Route::resource('autenticacao', 'AutenticacaoController');

Route::middleware(['auth'])->group(function(){
    Route::get('/', 'MunicipioController@inicio')->name('inicio');
    Route::get('/logout', 'AutenticacaoController@logout')->name('autenticacao.logout');
    Route::resource('municipios', 'MunicipioController');
});

If I access the route localhost/login, I can access it, but if I access it localhost/, get the bug:

Route [login] not defined.

What could be wrong?

The idea is that when accessing localhost/, be redirected to localhost/login

  • 1

    There was no shortage of creating a middleware to verify and redirect to this \login? how you did your authentication form, also need to create route protection, the one that comes in the framework is an example and follows a pattern

  • hi @virgilionovic I don’t know how to do. can you explain me? in the tutorial I’m following, the guy does similar to how I’m doing.

  • 1

    Qual tutorial @Italo?

  • @Virgilionovic is a course of Latin on the site of devmedia. I posted the question there too, but I think the guy only answers Monday :/

  • 1

    So maybe the guy blends what’s in Laravel with what he’s teaching. When I do that part, I mount my own middleware and my own login or logout route ... ! see I think any answer will get in your way (I think)

  • @Virgilionovic blz then. Still thanks for the tip

  • 1

    Any help can get in the way of the course ... does not have a focus to answer accurately ... I know a way, but if the course the guy uses another ? will not lose focus !

  • 1

    In your route settings, you have set that you need to be logged in to access localhost/, and if it is not, Laravel automatically redirects to a route with the name login by default. If you replace the autenticacao.login for login, I believe it will redirect correctly.

  • really worked Vinicius worth it

  • @Viniciuslourenço if you want, put the solution to earn the points

Show 5 more comments

1 answer

1


When you use the Laravel Standard Middleware it sets some name of standard routes. In your case, you have secured the route localhost/ but did not define the name as login on some other route so that Laravel could redirect when the user was not logged in.

Just change your mind:

Route::get('/login', 'AutenticacaoController@login')->name('autenticacao.login');

To:

Route::get('/login', 'AutenticacaoController@login')->name('login');

And detail, Laravel uses the function name to re-order, so long as it is login, you can set as you want your login route.

If you want to change other Auth routes, see this link.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.