doutu9810 2014-03-11 23: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 23:56
    关注

    in route

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

    in controller

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

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题