问题
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