Problem returning several Routes in the Laravel

Asked

Viewed 170 times

0

I have a route that when accessing www.exemplo.com/comer returns a view with the content. What I’m trying to do now and created another route to do the same effect but when I try to access www.exemplo.com/cesar-Sousa does not return me the right view returns the view of the first example I gave above.

Basically I want to know how I can create Routes to access several different views but with the position of the domain always equal.

Example

www.exemplo.com/comer ->EstabelecimentosController@estabelecimentos
www.exemplo.com/cesar-sousa -> PerfilUserController@perfil_user

I want something like that same position always following the bar but depending on changing the Lug that is stored in the database return the right controller and the right view.

Routes

Route::get('{slug_user_perfil}', 'PerfilUserController@perfil_user');
Route::get('{slug_categoria}', 'EstabelecimentosController@estabelecimentos');

And more or less this I have the slugs stored in the database I intend that depending on the Slug displayed in the URL he knows which controller to use and by itself will return the correct view by I put two Routes with Slug parameter but when I try to access Slug of the users it always returns the controller of the categories that returns the view of the categories and this is my problem

  • I think so, this is an error... If Voce put the name instead of the variable solves the problem....

  • But I can’t put the name because Slug is stored in the database so that’s why I’m using variable what I intend is which way to make each Slug that returns in the URL it knows which controller it has to use

  • It’s the following you put Route::get('{slug_user_perfil}', 'PerfilUserController@perfil_user'); two equal routes only that point to different places, and this can not! if you can do different type: Route::get('comer', 'PerfilUserController@perfil_user'); and Route::get('cesar-souza', 'EstabelecimentosController@estabelecimentos'); with are no variables will work as valid routes ... what you are trying to do will give crash routes and this can not and variable on root route is not a good practice, because it limits always be like this and can confuse the route systems as it already happens.

  • But it has to be by variable because these names are slugs stored in the database there has to be a way around this because imagine that I have 100 users I will not make 100 routes to home user I think does not make sense

1 answer

0

If I understand the question correctly, just create a function to test what was passed as parameter:

Route::get('/{param?}', function ($param="default") {
    if( $param == "default")
        return view('welcome')
    else if( $param == "comer")
        return view('comer')
    return view('cesar-sousa');
});
  • It is a way to predict the value of the route, but, this can bring more problems limiting the route systems always like this and then you lose the MVC of separation of responsibility having attached a controller for everything ... for a few routes or single page may be useful, but I still find it invalid if the system increases ...

Browser other questions tagged

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