URI control |
[eluser]Perkin5[/eluser]
I am building a shopping site and there is a facility to choose a category with a dropdown list. Choosing a category (call it 'greetings') links you to a controller method called 'category' which loads a page like this: Code: $data = array( The resulting URI is then www.site/shop/category I want it to be www.site/shop/category/greetings So I need to create a segment3 of the URI. How could I achieve that?
[eluser]Samus[/eluser]
This should give you an idea.. Code: class Shop extends CI_Controller { In this situation you now have an url: site.com/shop/category/something If you visit this url, it will echo 'something'. or if you visit site.com/shop/category/greetings it will echo 'greetings'. The example's a bit vague, but I hope that helps you understand how URLs are created with CI. Simply the value of your method is the third URI
[eluser]Perkin5[/eluser]
Don't follow this. The controller name ('shop' in this case) provides segment 1. The method ('category' in this case) provides segment 2. What will provide a segment 3? Where does the 'something' come from? The only time I've seen a segment 3 is with pagination where the offset is segment 3.
[eluser]Carmichael[/eluser]
Read about routes here: http://ellislab.com/codeigniter/user-gui...uting.html application/config/routes.php Code: $route['shop/category/:(any)'] = 'shop/category';
[eluser]Samus[/eluser]
[quote author="Perkin5" date="1333839642"]Don't follow this. The controller name ('shop' in this case) provides segment 1. The method ('category' in this case) provides segment 2. What will provide a segment 3? Where does the 'something' come from? The only time I've seen a segment 3 is with pagination where the offset is segment 3.[/quote] You have a method like this: Code: function($one, $two, $three) { $one then becomes the third parameter, $two becomes the fourth, $three becomes the fifth. I don't know how else I can explain this, as it's pretty much the basics of CI.
[eluser]Perkin5[/eluser]
Ah yes, I wasn't aware of this - thanks a lot. I will read this carefully, it clearly answers my question.
[eluser]Perkin5[/eluser]
Actually, having read it carefully, I don't think it does answer my question. URI routing appears to be a way of modifying an existing URI so it will link to a different method. That's not what I want. Can I put it another way? I can write: Code: redirect('model/method/'.$variable); and when 'method' is called, the URI is like this: Code: www.site.com/model/method/$variable So what I'm looking for is a way, within 'method', to add the third segment on to the URI just like I can do it with redirect. Is that possible?
[eluser]minerbog[/eluser]
[quote author="Samus" date="1333840983"][quote author="Perkin5" date="1333839642"]Don't follow this. The controller name ('shop' in this case) provides segment 1. The method ('category' in this case) provides segment 2. What will provide a segment 3? Where does the 'something' come from? The only time I've seen a segment 3 is with pagination where the offset is segment 3.[/quote] You have a method like this: Code: function($one, $two, $three) { $one then becomes the third parameter, $two becomes the fourth, $three becomes the fifth. I don't know how else I can explain this, as it's pretty much the basics of CI.[/quote] Now you learn something everyday!! I've been using CI for nearly two years and I didn't know that!! However, if you don't always need a third parameter the function will then break, which is why I would always get the segments via $this->uri->segments(n); But still great to know! :-) Gav.
[eluser]minerbog[/eluser]
[quote author="Perkin5" date="1333891717"]Actually, having read it carefully, I don't think it does answer my question. URI routing appears to be a way of modifying an existing URI so it will link to a different method. That's not what I want. Can I put it another way? I can write: Code: redirect('model/method/'.$variable); and when 'method' is called, the URI is like this: Code: www.site.com/model/method/$variable So what I'm looking for is a way, within 'method', to add the third segment on to the URI just like I can do it with redirect. Is that possible?[/quote] Sorry, really don't get what your trying to do 8-/
[eluser]Samus[/eluser]
[quote author="minerbog" date="1333893116"][quote author="Samus" date="1333840983"][quote author="Perkin5" date="1333839642"]Don't follow this. The controller name ('shop' in this case) provides segment 1. The method ('category' in this case) provides segment 2. What will provide a segment 3? Where does the 'something' come from? The only time I've seen a segment 3 is with pagination where the offset is segment 3.[/quote] You have a method like this: Code: function($one, $two, $three) { $one then becomes the third parameter, $two becomes the fourth, $three becomes the fifth. I don't know how else I can explain this, as it's pretty much the basics of CI.[/quote] Now you learn something everyday!! I've been using CI for nearly two years and I didn't know that!! However, if you don't always need a third parameter the function will then break, which is why I would always get the segments via $this->uri->segments(n); But still great to know! :-) Gav.[/quote] Or you can just give the function a default value. Code: function samus($one = '', $two = 'fourth') { |
Welcome Guest, Not a member yet? Register Sign In |