Reputation: 1472
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!
Upvotes: 3
Views: 314
Reputation: 102874
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).
Upvotes: 3