2

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.

tereško
  • 58,060
  • 25
  • 98
  • 150
vikrants
  • 43
  • 1
  • 6

2 Answers2

3

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);
}
Will Parker
  • 466
  • 2
  • 11
Thom
  • 263
  • 2
  • 13
  • thanks for your answer. I have more than one controller. Also, what if I need to pass additional segments to the URL, for example, http://www.mysite.com/[city]/[area]/[outlet-name]/[outlet-pictures]/123. Should I simply add two more parameters to the routing rule. – vikrants Jun 07 '12 at 20:43
  • You can either use the routes to pass the variables or you can test for them in the controller. I edited answer above to show passing more options. The URL will remain (mysite.com/[city]/[area]/[outlet-name]/[outlet-pictures]/123) so in the controller you can still test for segment 4 and 5. In your remap function you can call different functions based on your variables. – Thom Jun 07 '12 at 21:49
0

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.

user1418338
  • 254
  • 1
  • 2
  • 1
    My understanding is that $this->uri->segment() is used to retrieve segments of URLs. The problem that I am facing is not related to retrieving URL segments but setting up segments for loading a view. To clarify again, I want to build a URL like http://www.mysite.com/[city]/[area]/[outlet-name] that should load the outlet details view at http://www.mysite.com/outlets/123. The problem is how to pass the city and area name to the URL. It seems I need help with reverse routing. – vikrants Jun 07 '12 at 21:11