Routes in Codeigniter
Asked Answered
C

2

6

I want to have a clean URL in CodeIgniter based application for User's Profile Information.

Please Take a look at URL formats below.

Actual URL : http://www.mydomain.com/index.php/users/profile/user1

I'm expecting users to have Personal URL's like

http://www.mydomain.com/user1
http://www.mydomain.com/user2
http://www.mydomain.com/user3

URL http://www.mydomain.com/user1 should process http://www.mydomain.com/index.php/users/profile/user1 in background execution.

I will be removing index.php from URL using Route library.

Thanks in advance for any sort of help.

Crown answered 9/10, 2012 at 5:53 Comment(0)
M
10

Have a look at https://www.codeigniter.com/user_guide/general/routing.html.

$route['user(:num)'] = "users/profile/user/$1";

If you mean you want /anyusername to route to the users controller, you would have to put:

$route['(:any)'] = "users/profile/$1";

At the bottom of routes.php and every non user-URL above it. Otherwise every URL would be routed there, obviously. You will need to implement some mechanism in the users-controller to throw 404-errors, since you are routing all requests not catched in the routing rules above.

Mortal answered 9/10, 2012 at 6:10 Comment(3)
Somehow, I have a feeling the users' name can be any arbitrary combination of letters and numbers. (i.e. not user[0-9])Gemini
Your answer is what i have expected, now only one concern here which is - how actual URL (Non userprofile URL) will work or taken care of ? url can be mydomain.com/contact. Do you mean I have to write down and route each possible URL to overcome this situation ?Crown
You don't have to route every specific URL, but every /<whatever> section you have. Such as /contact/:any, /start/:any, etc.Bloodhound
M
1

IN config/routes.php

add this line

$route['user(:num)'] = "users/profile/$1";
Microcyte answered 9/10, 2012 at 6:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.