3

Is there any way I can make user URLs? something like

www.domain.com/username

I know I probably have to use a route system, and I do use this a lot for other URL requests. But the thing is, I need the username to dynamically create the user profile, if it exists. But do I also need to keep my other controller classes in mind?

Thanks!

1 Answer 1

3

I see you realize the potential for a user to step all over your other valid routes by creating a matching user name.

You could try routing all requests as usernames, but providing a trigger for your other controllers (and other valid routes):

// Route everything to users profile
$route['(:any)'] = 'users/profile/$1';

// Route all requests after "my_trigger" as normal
$route['my_trigger/(:any)'] = '$1';

* I'm not sure, but you may need to append more /(:any)s to the trigger routes.

This would mean that my_trigger would have to be an invalid username, but would be the only invalid user name.

The idea is that all non-username requests must be preceded with the additional "trigger" segment, otherwise it will be considered a user name. So /blog now must be accessed with /trigger/blog.

You could of course do the same with the user name instead, but end up with a less pretty url, like /u/username, or use a query string like ?u=username.

Another option is to specifically whitelist segments that should invoke a controller as normal, and blacklist them from the available user names, while routing all other requests through your users controller.

In any case, if the user name doesn't actually exist - you need to respond appropriately, so there is no real need for "dynamic" routes (i.e. creating a route for each and every user).

Sign up to request clarification or add additional context in comments.

2 Comments

You already got this running? I'm curious because I have never actually used this method of routing. This is working? Feel free to simply upvote, uncheck the accept, and leave the question open for a day or two - someone else may have valuable input.
BTW, try using a regular expression instead of :any for the route, makes more sense and will read the slashes instead of discard them.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.