1

I like to have the user’s full name in their profile page-url rather than the username in case they don’t want their username associated with their full name. In my case they don’t get to set the username at the sign up point but might been asked for a username by the person signing them up.

So, how do i use the user’s full name as an identifier in the url of their profile page?

Like so: example.com/user/first-last

Is that even possible?

2 Answers 2

2

Although the craft.users tag let you find users who have firstName or lastName matching whatever, I wouldn't try to match just against that, as you could end up having users with the same sames.

I would include the id of the user in the URL, so you would have something like example.com/user/1-first-last

Then you can lookup the profile by doing this:

  1. Add a dynamic route that matches for example profile/(number)-*
  2. Graf the profile with something like this:

    {# 
        - Use matches variable from dynamic route
        - matches.number will give you the expected id if you only have 
          one number segment in your dynamic route
    #}
    {% set userId = matches.number %}
    {# Find user #}
    {% if userId is defined %}
        {% set profile = craft.users.id(userId) | first %}
            {{ profile.name }}<br/>
            {{ profile.username }}<br/>
            {{ profile.email }}<br/>
            {{ profile.id }}<br/>
    {% endif %}
    
5
  • “as you could end up having users with the same sames” That’d definitely be an issue. Going with the id beforehand might be a good way to go. Thank you. Commented Sep 16, 2014 at 14:53
  • Good. In general, you want some kind of information that is sure to be unique to look up the user, like the id, username or e-mail. Commented Sep 17, 2014 at 10:50
  • @MarcusScheller Could you mark one of the answers as the accepted one? Commented Dec 20, 2014 at 19:00
  • We went with the username (identical names being an issue) so practically I haven’t further looked into any of the above. Commented Dec 22, 2014 at 21:26
  • I accepted yours now. Commented Jun 6, 2023 at 8:05
4

Marcus, I think you can use anything as an url for a user's page. In your case, I'd make a dynamic route like:

user/*

and assign it a template, for instance:

user/_entry

Within that template, all you'd have to do is lookup the user by the match found in the url. The variable matches contains the first-last part (the * part of your route). With that, the user should be easy to find:

{% set names = matches[1] | split('-') %}
{% set user = craft.users.firstName(names[0]).lastName(names[1]).first() %}

{% if user %}
    // Do with it what you want
{% endif %}

You'd have to be careful, however, that no two identical first- and last name combinations exists. The advantage of using the username is that it's always unique.

1
  • Thanks Paul. “two identical first- and last name combinations” -- True, so i might look into Fred’s suggestion using a combination of id and full name. Username would still be the fallback but just not preferred. At least i would not like that. Commented Sep 16, 2014 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.