Reputation: 1491
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.
If a user then types in sitename.com/search/cars
, the controller will look for the function cars
within the search
controller.
However I want to have a generic function called lookup(), which takes the 2nd parameter in a URL string.
For example: sitename.com/search/electronics [electronics is returned] sitename.com/search/cheese [cheese is returned]
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';
<?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()
{
}
}
?>
Upvotes: 0
Views: 468
Reputation: 6393
in route
$route['search/(:any)'] = "search/index/$1";
in controller
public function index($value)
{
//$value = $1
}
Upvotes: 1