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.

2 Answers 2

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your answer. I have more than one controller. Also, what if I need to pass additional segments to the URL, for example, mysite.com/[city]/[area]/[outlet-name]/[outlet-pictures]/123. Should I simply add two more parameters to the routing rule.
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.
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.

1 Comment

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 mysite.com/[city]/[area]/[outlet-name] that should load the outlet details view at 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.