codeigniter how to set routes to default controlle

2019-04-15 08:03发布

I am using codeigniter as a module in my project to display information as categories and its items.

I want the url's like the following

  1. sitename.com/module/ will display all items in random order.
  2. sitename.com/moduel/categoryname/ will display all items in that specific category
  3. sitename.com/module/categoryname/title-of-the-item/ this will display the details of that specific item

here sitename.com/module/ is the root of the codeigniter and the default controller is home.

my question is how to set routes for one segment or uri and two segments of uri like

  1. sitename.com/module/ should load default controller(home in my case)
  2. sitename.com/module/categoryname/ should load home/category/categoryname
  3. sitename.com/module/categoryname/title-of-the-item should load index/title/title-of-the-item

i will have defined my functions as follows

  1. funciton index() ...
  2. function category($categoryname) ....
  3. function title($title)....

so, either i want to create a route according to the num of segments or if there is any other alternate to accomplish this with routing then that is fine.

I want to do it using the routing and not by passing the 2 segments as arguments to Index function.

so, ?

2条回答
2楼-- · 2019-04-15 08:40

Set the base url of the site to sitename.com/module.

You could try database driven routing.
This works by using a database table to write your routes for you.

Example database table;

CREATE TABLE IF NOT EXISTS `app_routes` (
  `id` bigint(20) NOT NULL auto_increment,
  `category` varchar(192) collate utf8_unicode_ci NOT NULL,
  `controller` varchar(64) collate utf8_unicode_ci NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `category` (`category`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1;

Example routes;

$route[ 'default_controller' ]  = 'home';
$route[ '404_override' ]        = 'error404';

require_once( BASEPATH .'database/DB'. EXT );
$db =& DB();
$query = $db->get( 'app_routes' );
$result = $query->result();
foreach( $result as $row )
{
    $route[ $row->slug ]                 = 'home/category_lookup/'.$row->slug;
    $route[ $row->slug.'/:any' ]         = 'home/item_lookup/'$row->slug/$1;
    $route[ $row->controller ]           = 'error404';
    $route[ $row->controller.'/:any' ]   = 'error404';
}

This link will give you full details (it is not my work - Google is just my friend).

查看更多
倾城 Initia
3楼-- · 2019-04-15 08:44
$route['page/(:any)'] = "home/page/$1";
$route['page'] = "home/index";
$route['(:any)/page/(:any)'] = "home/category/$1/page/$2";
$route['(:any)/page'] = "home/category/$1";
$route['page'] = "home/index";

$route['(:any)/(:any)'] = "home/title/$2";
$route['(:any)'] = "home/category/$1";

the last two lines (7 and 8) is what i needed... and that served my purpose

the lines before the last two(1,2,3,4,5) is how i use pagination for the last two lines(7 and 8)

only home/category and home/ has pagination and not home/title which is the end page.

meanwhile, if anybody found the code could be buggy or need perfection then please add an answer so that i can update.

查看更多
登录 后发表回答