开发者

Codeigniter _remap function

开发者 https://www.devze.com 2023-01-14 14:19 出处:网络
Please help I want to use first URI segment into my CodeIgniter website. Like when I open these url开发者_Python百科 they opens my profile:

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

Like when I open these url开发者_Python百科 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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消