Customize dynamic URL.change ?id to name

左心房为你撑大大i 提交于 2019-12-12 04:55:55

问题


I'm using codeigniter for make dynamic website. In news page, I use url method like: http://test.com/test/test/news_details?id=27

how can i change my url to put news title within ?id=27

example:

http://test.com/test/test/news_details/pass-this-url

"pass-this-url" refer to id=27.

Best Regards.


回答1:


Use routes : http://www.codeigniter.com/user_guide/general/routing.html

In your case it will be something like this :

$route['pass-this-url'] = "test/test/news_details?id=27";

So http://www.example.com/pass-this-url will be understand by codeigniter as http://www.example.com/test/test/news_details?id=27

however, if you want to make it dynamic, you will have to call your db :

require_once( BASEPATH .'database/DB'. EXT );
$db =& DB();
$query = $db->get( 'news' );
$result = $query->result();
foreach( $result as $row )
{
    $route[$row->title] = "test/test/news_details?id=". $row->id;
    //We suppose here that your title is URL friendly.
} 



回答2:


Go to application/config/config.php and check this:

$config['enable_query_strings'] = FALSE; 


来源:https://stackoverflow.com/questions/28537819/customize-dynamic-url-change-id-to-name

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