CodeIgniter自定义URI路由


CodeIgniter Custom URI Routing

我想在CodeIgniter上创建自定义永久链接,实际上我买了脚本,但开发人员由于一些漠不关心而离开了该项目。所以现在的问题是,我不知道如何改变该脚本上的永久链接。主要的永久链接问题是,当我在搜索栏上搜索任何东西时,我得到这个url:

domain.com/? s = xxxxx%20yyyyy

我想要这样的url结构:

domain.com/search/xxxxxx-yyyyy/

应用程序/配置/routes.php

$route['default_controller']    = "music";
$route['404_override']          = '';
$route['search/(:any)']         = "music/index/$0/$1/$2";
$route['search/music/(:any)']   = "music/$1";

我猜你想要的是不可能(直接)
假设您的表单为

<form action="" method="GET">
    <input type="text" name="s" value="" placeholder="Search music..." />
</form>

由于方法是GET,默认功能说要在URL中添加参数作为查询字符串。

作为规范(RFC1866,第46页;HTML 4。X section 17.13.3) state:

如果方法是"get",操作是HTTP URI,则用户代理获取action的值,并附加一个' ?',然后附加表单数据集,使用"application/x-www-form-urlencoded"内容类型进行编码。


基本上你在这里能做的是应用一个hack到这个。在应用搜索时将用户重定向到所需的URL。
你可以这样做,

控制器

(控制器/music.php)

<?php
class Music extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('xyz_model');
    }
    public function index()
    {
        if($this->input->get('s'))
        {
             $s = $this->input->get('s');
             redirect('/search/'$s);
        }
        $this->load->view('home.php');
    }

    public function search()
    {
        $s = $this->uri->segment(2);
        /*
        Now you got your search parameter.
        Search in your models and display the results.
       */
       $data['search_results'] = $this->xyz_model->get_search($s);
       $this->load->view('search_results.php', $data);
    }
}