Welcome Guest, Not a member yet? Register   Sign In
Building Codeigniter URLs with variable segments
#1

[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.
#2

[eluser]Glazz[/eluser]
Have you read http://ellislab.com/codeigniter/user-gui...uting.html ?
#3

[eluser]vikrantsharma1[/eluser]
[quote author="Glazz" date="1339279971"]Have you read http://ellislab.com/codeigniter/user-gui...uting.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?
#4

[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
#5

[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?
#6

[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"'); ?>
#7

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




Theme © iAndrew 2016 - Forum software by © MyBB