Route envelope

Asked

Viewed 108 times

1

In my file Routes > web.php I have the following routes:

Route::get('/user', (...));
Route::get('/user/{id}', (...) );
Route::get('/user/{id}/{nome}', (...) ); // retorna o nome e id do usuário
Route::get('/user/{id}/perfil', (...) ); // retorna o perfil do usuário

When I try to access the last route, Laravel always solves the routing by returning the comments... how I could leave the same route prefix with user/id but accessing the route referring to the profile ?

2 answers

3

Hello, Kasio!

If you have similar routes, but receive parameters, the ones you receive should come last.

And you can also group these routes into a group of routes, leaving them more "clean", and less repetitive:

Example:

<?php
//...
Route::group(['prefix' => 'user'], function() {
    Route::get('/', 'controller@method'); // Rota: /user
    Route::get('/{id}', 'controller@method'); // Rota: /user/{id}
    Route::get('/{id}/perfil', 'controller@method'); // Rota: /user/{id}/perfil
    Route::get('/{id}/{nome}', 'controller@method'); // Rota: /user/{id}/{nome}
});

3

Just reverse the routes, like this:

Route::get('/user', (...));
Route::get('/user/{id}', (...) );
Route::get('/user/{id}/perfil', (...) ); // retorna o perfil do usuário
Route::get('/user/{id}/{nome}', (...) ); // retorna o nome e id do usuário
  • Thanks ! I will try André! : D ; - So the routes with "parameters" I should always call after?

  • That Kasio, you have to call later or else you will always understand as parameter.

  • Problem solved?

  • Yes! Thank you Andy!

  • Great, don’t forget to put the answer with the right one, to help others in the future.

Browser other questions tagged

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