LondonGuy
LondonGuy

Reputation: 11098

Codeigniter: I would like create user URL's for my users when they create a username

I wold like to create this kind of URL for my users when they register a username mysite.com/username

I've search many posts on this site and I see people doing it like this mysite.com/user/username

I would like to get rid of the user and just have it go directly to mysite.com/username

What would be the best way to this?

Upvotes: 0

Views: 1438

Answers (3)

Matthew Rapati
Matthew Rapati

Reputation: 5686

You can do this by adding an item to the $route array in 'config/routes.php':

$route['([a-zA-Z0-9_-]+)'] = "user/index/$1";

This maps example.com/whatever to to the 'index' function of the 'user' controller.

Yes, there will be collisions if you have a 'logout' controller and a user named 'logout'. You can solve this by putting adding the following item to the $route array before the one above:

$route['logout'] = "logout";

And keeping a blacklist of usernames that includes your controllers. This will take some upkeep, so you may want to go with the other solutions listed here that prefix something like 'user/' in the URL before their username and avoid all that.

Upvotes: 2

Knossos
Knossos

Reputation: 16038

You could look into routes:

http://codeigniter.com/user_guide/general/routing.html

You enter routes into config/routes.php

As an example:

$route['([a-zA-Z]+)'] = "main_controller/user_function/$1";

But as @Nylon Smile said above, if you attempt to use logout, it will try to access logouts' user page.

Upvotes: 5

Nylon Smile
Nylon Smile

Reputation: 9436

I am not familiar with CodeIgniter but here is a general answer for why people don't use mysite.com/username:

mysite.com/user/username is a good solution because usually the URL dispatcher passes username to a function/module that handles users' page.

mysite.com/username is a bad solution because your dispatcher will have to have a greedy rule after all your other rules, to pass everything other that those previous rules to the module/function that handles users' page.

Now suppose you want to use mysite.com/username and suppose you have mysite.com/logout which lands users to the logout page. A user comes and registers himself with "logout" as his username. Then how are you going to say by accessing mysite.com/logout Mr. logout wants to logout or wants to see his page?

Upvotes: 2

Related Questions