changing url displayed to user in codeigniter

别来无恙 提交于 2020-01-25 08:13:28

问题


I don't know if this is strange request or not, but I have a page with users profiles displaying. Each user has unique username of course and those are included in links to associated with their names. when clicked it goes to their profile:

profile/profile_guest/username

When the user clicks on one of the profiles, it goes to user's profile and url in the address bar is:

http://domain.com/profile/profile_guest/ahlam

What I want to achieve is to configure routes.php to handle this. so once clicked, it goes to profile page but what shows in URL address bar is :

domain.com/ahlam

To do that, I tried :

$route['someTest'] = "profile/profile_guest/$1";

Fair to say that it didn't work and still the whole URL displayed. What can I do to fix this?

Thanks all


回答1:


You should use something like

$route['sometest/(:any)'] = "your/link/$1";

The '$1' is like a parameter you gonna receive from the '(:any)' part.




回答2:


//site.com/john          = "profile/profile_gest/john"
$route['([a-z0-9\-_]+)'] = "profile/profile_gest/$1";
//where $1 = "john", so "john" respects the pattern set below

But CI Routing will understand any URI match this pattern like "index", "post", ... is a routing for the profile controller.

TO prevent that you can add a specification to the url, for examples:

//site.com/john.html
$route['([a-z0-9\-_]+)\.html'] = "profile/profile_gest/$1";

//site.com/user-john
$route['user\-([a-z0-9\-_]+)'] = "profile/profile_gest/$1";

Or

if you are sure that any usernames will not contest with an another pages always put this rules at the bottom, in order to that CI will check all other routing rules before this one.

Note: Before that your htaccess have to be set to remove index.php from your urls.

CI URI Routing

Regular expression



来源:https://stackoverflow.com/questions/16152441/changing-url-displayed-to-user-in-codeigniter

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