使用codeigniter的URL路由 [英] URL routing with codeigniter

查看:106
本文介绍了使用codeigniter的URL路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 search 的控制器。如果用户键入 sitename.com/search ,它会点击 search 控制器并运行 index 函数。

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.

如果用户然后键入 sitename.com/search/cars ,控制器将查找函数搜索控制器中的code> cars

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

有一个称为lookup()的通用函数,它使用URL字符串中的第2个参数。

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


例如:sitename.com/search/ electronics [electronics is
returned] com / search / cheese [返回奶酪]

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

然后使用关键字if它找到一个匹配它加载页面。在汽车的情况下,它将 sitename.com/search/cars 如果没有匹配,则它重定向到 sitename.com/search/error

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()
       {

       }


    }
    ?>

推荐答案

b $ b

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

        public function index($value)
    {
        //$value = $1

    }

这篇关于使用codeigniter的URL路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆