Reputation: 175
Im not to good in php and codeigniter gives me maximum. My site is a search engine and after i do a search, url of my site is like this: mysite.com/?search=eminem&type=mp3 and i don't like it. I want to do it so when i type search eminem to give me this url: mysite.com/eminem/mp3 Here is code from 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);
}
}
what other code do i have to add here to transform the qwery strings to paths?
here is routes.php
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
$route['404_override'] = '';
Here is my search form from 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>
here is .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>
Upvotes: 0
Views: 605
Reputation: 7593
Assuming mod_rewrite is already working, then you can set this in your routes.php
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
$route['(:any)/(:any)'] = "pages/search/$1/$2";
I'm differentiating pages/search
from pages/view
so it would be less confusing.
By the way, this has to be the last route in your routes.php file so it has low priority and won't conflict with specific routes like the samples below. Because if it's on top of your routes file, then accessing users/list
will be treated as search=users&type=list
.
$route['users/list'] = "users/list";
$route['pages/list'] = "pages/list";
Then in your pages controller.
public function search($search = '', $type = '')
{
// $search now contains $1 and $type contains $2
// instead of $_GET['search'] and $_GET['type']
}
Hope that helps.
Upvotes: 1