Codeigniter路由:控制器功能和变量冲突

时间:2014-09-08 15:36:20

标签: php codeigniter codeigniter-routing

我正在尝试将此代码简化为更少的行。

// Controller function routing
$route['news/popular'] = "news/popular";
$route['news/featured'] = "news/featured";
$route['news/latest'] = "news/latest";

$route['news/index/(:any)'] = "news/index/$1"; // points to a url slug of article
$route['news/(:any)'] = "news/index/$1"; // points to article without 'index' segment

对此:

$route['news/(:any)'] = "news/$1";
$route['news/(:any)'] = "news/index/$1";
$route['news/index/(:any)'] = "news/index/$1";

根本不工作。还有其他方法吗?或者我应该坚持使用长代码?

2 个答案:

答案 0 :(得分:0)

你不能为两个控制器提供相同的路线....

$route['news'] = "news/latest";
$route['news/(:any)'] = "news/index/$1";
$route['news/index/(:any)'] = "news/index/$1";

答案 1 :(得分:0)

不,你不能在codeigniter路由中这样做,因为codeigniter如何猜测你想要使用哪个函数。

您可以尝试减少路线的方法是将新闻归类为单一功能。这样的事情:

   function get_news($news_type)
    {
       //check here which type of news is required and process accordingly.
    }

在你的路线中它就像这样

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

希望这能帮到你