CodeIgniter Forums
Building Codeigniter URLs with variable segments - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Building Codeigniter URLs with variable segments (/showthread.php?tid=52416)



Building Codeigniter URLs with variable segments - El Forum - 06-09-2012

[eluser]vikrantsharma1[/eluser]
I have searched for the solution to my problem in CI user guide but couldn't find any. 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_name]/[area_name]/[outlet_name].

The URL segments city and area are fields in their respective tables and outlet_name is a field in the table "Outlets".

Thanks for your help.


Building Codeigniter URLs with variable segments - El Forum - 06-09-2012

[eluser]Glazz[/eluser]
Have you read http://ellislab.com/codeigniter/user-guide/general/routing.html ?


Building Codeigniter URLs with variable segments - El Forum - 06-09-2012

[eluser]vikrantsharma1[/eluser]
[quote author="Glazz" date="1339279971"]Have you read http://ellislab.com/codeigniter/user-guide/general/routing.html ?[/quote]

Yes, and 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?


Building Codeigniter URLs with variable segments - El Forum - 06-09-2012

[eluser]InsiteFX[/eluser]
Code:
------------- Segments    1          2          3            4
http://www.mysite.com/[outlets]/[city_name]/[area_name]/[outlet_name].

// outlets controller
public function index()
{
    // NOTE: The second parameter 0 is a default value.
    $city_name   = $this->uri->segment(2, 0);
    $area_name   = $this->uri->segment(3, 0);
    $outlet_name = $this->uri->segment(4, 0);
}

By default it will return FALSE if the segment is empty.

CodeIgniter Users Guide - URI Class



Building Codeigniter URLs with variable segments - El Forum - 06-10-2012

[eluser]vikrantsharma1[/eluser]
[quote author="InsiteFX" date="1339286903"]
Code:
------------- Segments    1          2          3            4
http://www.mysite.com/[outlets]/[city_name]/[area_name]/[outlet_name].

// outlets controller
public function index()
{
    // NOTE: The second parameter 0 is a default value.
    $city_name   = $this->uri->segment(2, 0);
    $area_name   = $this->uri->segment(3, 0);
    $outlet_name = $this->uri->segment(4, 0);
}
[/quote]
Thanks for responding. Actually, I am new to this routes thing and pretty confused. As far as my understanding goes, the function that you have built will retrieve URI segments and based on that some results can be shown. My question is slightly different and let me try to explain it again.

Let's say I generate a list of all outlets with
Code:
$query = $this->db->select('outlet_name')->from('outlets')->limit(10, 0)
and then print them with a foreach loop with each outlet_name being a link to view details of the outlet.

Each of this link should look like http://www.mysite.com/[outlets]/[city_name]/[area_name]/[outlet_name]. My question is how do I generate such a link?



Building Codeigniter URLs with variable segments - El Forum - 06-10-2012

[eluser]InsiteFX[/eluser]
You will need to use the names form your foreach loop.
Code:
<?php echo anchor('site/'.$city_name, 'My Cities', 'title="Cities"'); ?>

<?php echo anchor('site/'.$city_name.'/'.$area_name, 'My City Area', 'title="City Area"'); ?>

<?php echo anchor('site/'.$city_name.'/'.$area_name.'/'.$outlet_name, 'My City Area Outlet', 'title="City Area Outlet"'); ?>



Building Codeigniter URLs with variable segments - El Forum - 06-10-2012

[eluser]vikrantsharma1[/eluser]
Thanks @InsiteFX. Will try out your solution.