dtz88967 2014-09-08 15:36
浏览 35
已采纳

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

I'm trying to simplify this code to a fewer lines.

// 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

To this:

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

Not working at all. Are there any other methods for this? or should I stick to the long code?

  • 写回答

2条回答 默认 最新

  • dton37910 2014-09-08 15:58
    关注

    No you can't do that way in codeigniter routes as how can codeigniter guess which function you want to use.

    What you can try to reduce your routes is categorize news in single function.Something like this :

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

    In your route it goes like this

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

    Hope this help you

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?