我不擅长PHP和codeigniter给我最大化。我的网站是一个搜索引擎,在我搜索之后,我的网站的网址是这样的: mysite.com/?search=eminem&type=mp3 ,我不喜欢它。 我想这样做,所以当我输入搜索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>
答案 0 :(得分:1)
假设mod_rewrite已经有效,那么您可以在routes.php
中设置它$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
$route['(:any)/(:any)'] = "pages/search/$1/$2";
我将pages/search
与pages/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']
}
希望有所帮助。