How to do categories in Codeigniter?

时光总嘲笑我的痴心妄想 提交于 2019-12-11 11:48:58

问题


I store the categories that a post belongs to in a database and I'd like to use them in the URL. How can I do this dynamically?

For example, I'd like to generate a URL like example.com/action-adventure/post-name.

I'd imagine it would involve routes.php, but I just don't know how to do it dynamically.


回答1:


This could be problematic, consider:

$route['(:any)'] = 'blog/post/$1';
$route['(:any)/(:any)'] = 'blog/post_category/$1/$2';

-

$route['(:any)'] = 'category/$1';

Blog/post would have priority here, so naming conventions become an issue/or not

Structure your links before hand I would suggest

public function post_category($type, $post_name){}



回答2:


An alternative way (not messing with routes) is to grab the uri segments. Your url will look something like

http://example.com/controller/index/category/post

And in your php, you can grab the category and post like

$category = $this->uri->segment(3);
$post = $this->uri->segment(4);


来源:https://stackoverflow.com/questions/9574743/how-to-do-categories-in-codeigniter

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