How can I achieve this URI scheme in CodeIgniter?
I'm making a blog site with CodeIgniter, and I'd like to use the first URI segment as a dynamic category.
So all of these URI's: http://example.com/category1/some-post http://example.com/category2/some-post http://example.com/category3/some-post
Would all go to the "categories" controller, where it would find the category from the database.
I can achieve this easily enou开发者_开发知识库gh with $routes['(:any)'] = 'categories';
, but of course then all of my other static controllers wouldn't work, such as admin, user, etc.
So how can I achieve this URI scheme while allowing other static pages to not be routed?
You can't unless you create specific routes for each category manually.
For instance:
$route['php'] = 'categories';
$route['java'] = 'categories';
If you look at wordpress they start categories with /category/
and all pages are anything else so you should probably do it that way round.
You will, as Francois said, have to create a route for each category. This can be slightly simplified with a simple array as below:
$categories = array('cat1','cat2','etc...');
foreach($categories as $cat)
{
$routes["$cat/(:any)"] = "category/$cat/\$1"
}
A possible alternative is to use a pre-controller hook in which you could query your database to see if the segment is a category and then direct the request appropriately.
If you'll have a lot of categories or they will be added to and updated frequently, I'd recommend the second option, otherwise, maintaining a simple array isn't too bad.
The other option is to declare all your static controllers in your routes...
精彩评论