Search code examples
routeslaravel-5friendly-urlslug

Is it possible in laravel 5 to show a pretty url to the user, and a practical url to the app?


I have this url: mywebsite.com/user/1/edit

I want my users to see this: mywebsite.com/edit-your-profile/

Is this possible when using Route::resource('user', 'UserController');? If yes, how do I do it :) ? I still want my app to be able to see user/1/edit as I use it in my middleware to prevent unauthorized access:

if ( \Auth::user()->id != $request->segment(2) ) {

    return  redirect()->back();

}

So, one pretty url for my user, a practical one for my app.


Solution

  • Based on your explanation, I take that a user can only edit their own profile. So mywebsite.com/user/1/edit should NOT even be allowed. Instead, add this in your Route.php add: Route::get('/edit-your-profile','UserController@edit'); and hardcode \Auth::user()->id into your editController and do not even allow the user to set the id. Why bother asking them for an ID if you already know what the id must be and ALL other ids will be rejected!

    PS. The html form for update should be at @update and the output that form should be sent to @edit. I just wanted to simplify the routing in the example.