Codeigniter _remap function

半腔热情 提交于 2019-11-27 21:29:11

问题


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:


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



来源:https://stackoverflow.com/questions/3614705/codeigniter-remap-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!