views:

87

answers:

1

Please help I want to use first URI segment into my CodeIgniter website.

Like when I open these url they opens my profile: http://www.facebook.com/buddyforever or http://www.myspace.com/zarpio

How can I do this with CodeIgniter? I checked _remap function but first coming controller how to hide controller?

+1  A: 

You can do this using the URL routing of codeigniter...

If you want your URL to be http://www.mydomain.com/zarpio and you want it to refer to your_controller, then do the following.

/config/routes.php

$route['(.*)'] = "your_controller/$1"; // Now, `zarpio` will be passed to `your_controller`

You can access it in your controller like this...

$my_name = $this->uri->rsegment(2);

However I do not suggest this way of configuring URLs. A better way would be...

$route['users/(.*)'] = "your_controller/$1";

This way, you're just renaming your controller name your_controller to users.

If you want to access profile of a user, you can do it like this...

$route['users/profile/(.*)'] = "another_controller/method/$1";
$route['users/(.*)'] = "your_controller/$1";

Consider the order of routing. Since you wrote users/(.*) in your route, it will match users/zarpio as well as users/profile/zarpio, and route both of them to your_controller/$1, which in the case of profile will give you a 404 page not found error. That is why you need to write users/profile/(.*) before users/(.*) in your routing configuration.

More information in codeigniter URI class documentation

ShiVik
Please, Can you give me full tutorial?
zarpio
Thank you ShiVik I did it. You are genius
zarpio
@Zarpio - No problem. You can mark the answer as correct one now, so that other users can follow up if they need it.
ShiVik
@Zarpio - The way to mark an answer correct is by clicking the "check box" sign that appears to the left of answer. I think you mistakenly think that clicking the upvote icon is marking the answer correct. Mark the answers correct for your past questions as well to increase your accept rate, so that you get more answers in the future.
ShiVik
oh ya I mark this one as correct answer... Thank you
zarpio
@ShiVik Can you please solve my another problem? I am using $route['(.*)'] = "your_controller/$1"; into my routes.php now problem is that when I goto www.mydomain.com/myanothercontroller/method... 404 Page Not Found comes... is there anything else to do in config/routes.php file?
zarpio
@zarpio - See the updated answer.
ShiVik
@ShiVik Thankyou Boss it helped me alot
zarpio