Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Birdy's avatar

Friendly URL's by displaying name apose to ID...

Hi all, I know this has been covered in similar questions but i can’t find anything that is exactly how i need it to be which leaves me wondering if its possible or not. My knowledge of laravel is rather limited and I’m by no means an expert in php or any other language! One would suggest a beginner status is appropriate in my skill set!

What I am actually trying to do is retrieve a record from my database without having to send the ID in the url or anyother numeric identifier, I was hoping to be able to have the url structure something as follows:

www.example.com/profiles/John-Doe

I know I could do a simple query to get either the username or full name but there is two reasons Im hoping there could be a workaround, The first reason is the user signs up with Email and Password followed by First name and Last name input fields and the second reason is that there could be another user name John Doe.

Right now my urls look like this: www.example.com/profiles/12101/John-Doe with the number being the unique identifier.

Would it be possible to switch this out and refer to the profile by name but still lookup the database using the id field? Surely some of the biggest websites out there that hide numeric fields and replace them with named fields are using some sort of switch method or something that can achieve a similar approach?

In theory im basically trying to mask the url or trick the url as I don’t want someone to be able to just swop out the name like so: www.exampe.com/profiles/John-Doe for www.example.com/profiles/Mary-Jane and be able to access the next profile so using a unique identifier like the first example I show is something I would prefer to have as the database query but just mask the url with the name if this is possible in anyway?

Much appreciated for any help or input given. Thanks for your time.

0 likes
10 replies
Snapey's avatar

How does someone reach the page? Can you give more context?

d3xt3r's avatar

Also would like to know,

Surely some of the biggest websites out there that hide numeric fields and replace them with named fields are using some sort of switch method or something that can achieve a similar approach?

which sites follows similar approach, highly SE unfriendly

Birdy's avatar

@jlrdw - I never even thought of doing that?! Might be tricky though passing it to the session as if the same user happens to click another profile then i would have to continually push the new value to the same session key, am i right?


@Snapey - They can either click through using href, buttons or similar and alternatively nothing stopping them using general get request.


Example:

http://www.example.com/profiles/178372/John-Doe just doesn’t look right does it? John-Doe might match the real name of the profile but its mainly just a dynamic parameter that doesn’t make any different its more the /178372/ unique id number that the query runs on but if i can pass that without having it in the url so in fact the correct url would be: http://www.example.com/profiles/John-Doe but in reality somehow the query is switching the name in the request for the relevant unique id behind the scenes.

Does this make sense or am i baffling you? As a newbie im not sure on the correct terms and phrases to use when it comes to syntax and mark-up so i give my apologies on that.

Thanks

ohffs's avatar

If you store the username 'slug' on the model when they create their account, then if I'm reading you right, you're wanting to do something like :

routes.php
Route::get('/profile/{user_slug}', 'UserController@profile');

UserController
public function profile($user_slug)
{
    $user = User::where('slug', $user_slug)->first();
    if (!$user) {
       abort(404);
    }
    return view('user.profile', compact($user));
}

And when you're creating a user (or saving one) :

$user->slug = str_slug($user->forename . ' ' . $user->surname);

You'd have to make sure the slug is unique of course (maybe as simple as adding a number if someone has already taken it), but I think that's the general idea. I don't think there's a URL-only way of handling your original question without some kind of unique feature to the URL. And doing it with hidden cookie/session things might be more confusing as two different people sending a link might end up on different profiles?

Snapey's avatar

As @ohffs says, just search for the slug. Its not 'REST' and it does not give you your requirement of the URL not being guessable, but you have conflicting requirements.

In theory im basically trying to mask the url or trick the url as I don’t want someone to be able to just swop out the name like so: www.exampe.com/profiles/John-Doe for www.example.com/profiles/Mary-Jane and be able to access the next profile so using a unique identifier like the first example I show is something I would prefer to have as the database query but just mask the url with the name if this is possible in anyway?

Do you want friendly URLs or do you want obscurity? You can't have both.

jlrdw's avatar

Even if someone switched the URL from Mary to John, they shouldn't be able to do anything In sensitive data if Mary is properly logged in. But how could they anyway unless Mary leaves the browser unattended and not logged out.
Mary should definitely log out for that bathroom break.
@Snapey are slugs just a key value combination like dbase used to use for a quick referencing in a table?

Snapey's avatar

@jlrdw no, a slug is just a web-safe url. Like the title of this post "friendly-urls-by...." Using a slug instead of an id means that for instance, this thread might have a thread ID, but also a slug that can be used to uniquely refer to the thread. Jeffrey will have something to stop two threads having the same name, such as a random number on the end of the duplicate. The next thing is to make sure the database has an index for the slug column so that threads can be found quickly.

Using this forum again as an example, every user creating an account needs to have a unique username. Threads by a particular person can be found by first searching for their name in the user table and then searching for that user's ID in the threads.

I know that laracasts.com/@jlrdw will take me to your profile page, so this is not safe if you want to stop people just walking through all the profiles. Its harder than with plain IDs but not impossible. Hence my comment about obscurity.

Birdy's avatar

@Snapey - Ideally i would like to have friendly urls as appose to having a unique id of something like: 3re23rf43v4-2424x2zs245-2saa2a-5nsgd5532s in the url to identify the users UUID, so something like i mentioned with .../profiles/John-Doe would be ideal but the only way i could do that is create a slug based on their firstname-lastname and insert it to a slug collumn within the DB and if its taken then use something like John-Doe-19. Guess all angles of input is better than one mind doing overtime!

Please or to participate in this conversation.