Reputation: 43
I have searched for the solution to my problem in CI user guide and on Stackoverflow as well but couldn't find. So, here is my problem.
I need to build SEO friendly URLs. I have a controller called "Outlets" and the view that I need to generate will have a URL structure like http://www.mysite.com/[city]/[area]/[outlet-name].
The segments city and area are fields in their respective tables and outlet_name is a field in the table "Outlets".
I am able to generate a URL like http://www.mysite.com/outlets/123 but how do I add the city and area name to the URL.
Upvotes: 2
Views: 2972
Reputation: 263
If all of your page use the same controller, in config/routes.php add this...
$route['(:any)/(:any)/(:any)'] = "outlets/$1/$2/$3";
$route['(:any)/(:any)/(:any)/(:any)'] = "outlets/$1/$2/$3/$4"; // fixed typo
In the controller you will want to remap the function calls because Codeigniter will be looking for functions with the names of the city and they will not exist.
http://codeigniter.com/user_guide/general/controllers.html#remapping
public function _remap($city, $area, $outlet, $options = '')
{
$this->some_function_below($city, $area, $outlet, $options);
}
Upvotes: 3
Reputation: 254
Another alternative solution.
You can use URI segments. For an url like http://www.mysite.com/[city]/[area]/[outlet-name]
<?php
$this->uri->segment(3); // city
$this->uri->segment(4); // area
$this->uri->segment(5); // outlet-name
?>
and so on... See http://codeigniter.com/user_guide/libraries/uri.html for more details.
Upvotes: 0