我有文章控制器和视图功能来显示单个文章,所以我的网址就像
http://domain.com/articles/view/<article-id>/<article-title>
我的代码:
class Articles extends CI_Controller {
function view($id=NULL,$slug=""){
//Code to fetch article details from DB by id
}
}
如何让我的网址看起来像http://domain.com/<article-title>
谢谢。
答案 0 :(得分:3)
将路由配置中的所有控制器定义为自己的方法。在路由配置结束时添加以下规则 -
$route['(:any)'] = 'articles/view/$1';
现在,artcile/view
控制器方法将提供除先前定义的路由之外的所有请求。
下一部分是创建一个映射表,用于将文章标题映射到文章ID。你可以用
获得文章标题$this->uri->segment(1);
在view
函数中。
每当使用title更新文章时,也要更新映射表。
答案 1 :(得分:1)
我为我的博客做了类似的事情。
@Varun根据Sachin的评论,这是强调路由路径 domain.com/articles/view/ 实际上您会按预期将
此外,我已经延长了像这样的级别网址
$route['spring/(:any)'] = "controller_name/method_name/$1";
$route['hibernate/(:any)'] = "controller_name/method_name/$1";
所以实际网址看起来像这样
domainname.com/spring/spring-jdbc-example which is mapped to "controller_name/method_name/$1"