doutu9810 2014-03-11 15:52 采纳率: 0%
浏览 37
已采纳

使用codeigniter进行URL路由

I have a controller named search. Codeigniter works in the following way if a user types sitename.com/search it hits the search controller and runs the index function.

If a user then types in sitename.com/search/cars, the controller will look for the function cars within the search controller.

However I want to have a generic function called lookup(), which takes the 2nd parameter in a URL string.

For example: sitename.com/search/electronics [electronics is returned] sitename.com/search/cheese [cheese is returned]

Then it does a database lookup using the keyword if it finds a match it loads the page. In the case of cars it would be sitename.com/search/cars if no match then it redirects to sitename.com/search/error.

Is it possible to modify my controller to handle requests like this? Without specifying every possible route?

$route['Cars'] = 'sitename.com/search/Cars';  
$route['Cheese'] = 'sitename.com/search/Cheese';  
$route['Electronics'] = 'sitename.com/search/Electronics';  

Search Controller:

    <?php
    class Search extends CI_Controller {


     public function __construct()
           {
                parent::__construct();

        $this->load->helper('url');

//parse URL: run lookup() function then redirect to page if valid return


           }


        public function index()
        {
        //check for url string to see what set or collection to load:

        }

       public function lookup()
       {

       }


    }
    ?>

展开全部

  • 写回答

1条回答 默认 最新

  • dre26973 2014-03-11 15:56
    关注

    in route

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

    in controller

            public function index($value)
        {
            //$value = $1
    
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

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

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

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

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

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

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

客服 返回
顶部