Customize dynamic URL.change ?id to name

2019-09-03 10:23发布

问题:

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;