0

我不擅长 php,而 codeigniter 给了我最大的帮助。我的网站是一个搜索引擎,在我进行搜索后,我的网站的网址是这样的:mysite.com/ ?search=eminem&type=mp3 ,我不喜欢它。我想这样做,所以当我输入 search eminem 给我这个网址时:mysite.com/eminem/mp3 这是来自 CI 的代码

<?php

class Pages extends CI_Controller {

    public function view($page = 'home')
    {
        if ( ! file_exists('application/views/pages/'.$page.'.php'))
    {
        // Whoops, we don't have a page for that!
        show_404();
    }

    $data['title'] = ucfirst($page); // Capitalize the first letter
    $this->load->view('templates/header', $data);
    $this->load->view('pages/'.$page, $data);
    $this->load->view('templates/footer', $data);
    }
}

我必须在此处添加哪些其他代码才能将 qwery 字符串转换为路径?

这是routes.php

$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';

$route['404_override'] = '';

这是我来自 home.php 的搜索表单

<form id="searchwrapper" name="searchform">                                          <input type="text" class="searchbox" id="txtSearch" name="search" />                                        <input type="image" src="../images/empty.png" class="searchbox_submit"/>                                   

<p id="circ">
   <input  type="radio" id="mp3" name="type" checked value="mp3"/>
   <label class="circ-btn normal" for="mp3">Mp3</label></p>
<p id="circ">                   
   <input type="radio" id="album" name="type" <?php echo $radio2;?> value="album"/>
   <label class="circ-btn normal" for="album">Album</label></p>
</form>

这是.htaccess

<IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /ci/
        #Checks to see if the user is attempting to access a valid file,
        #such as an image or css document, if this isn't true it sends the
        #request to index.php
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
4

1 回答 1

1

假设 mod_rewrite 已经在工作,那么你可以在你的 routes.php 中设置它

$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
$route['(:any)/(:any)'] = "pages/search/$1/$2";

我正在区分pages/searchpages/view所以它不会那么混乱。顺便说一句,这必须是您的 routes.php 文件中的最后一条路由,因此它的优先级较低,并且不会与以下示例中的特定路由冲突。因为如果它在您的路由文件之上,那么访问users/list将被视为search=users&type=list.

$route['users/list'] = "users/list";
$route['pages/list'] = "pages/list";

然后在您的页面控制器中。

public function search($search = '', $type = '')
{
   // $search now contains $1 and $type contains $2
   // instead of $_GET['search'] and $_GET['type']
}

希望有帮助。

于 2012-10-06T04:45:53.107 回答