dtz88967 2014-09-08 07: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 07: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部