URI confusion |
[eluser]Unknown[/eluser]
Hello. I have been using CI for a few months and I am on my first major site build. My problem is as follows. The site I am building is a product catalog. I have a menu which I coded into a view called menu.php, which is then loaded into other view pages as needed. The links in this menu have the following structure: Code: "index.php/costumes/animal" My intent was to pass the variable "animal" to a function in my costumes.php controller. In reading the user guide, I thought I could accomplish this via the routes.php file. I did this so that I can have an informative URL in the address bar, instead of the default function/controller/variable URL. I have set up a route in routes.php: Code: $route['costumes/(:any)'] = "costumes/product_lookup/$1"; All was working fine. The intended variable "animal" was being passed through to the product_lookup function of my costumes controller; however when the subsequent view is loaded and I include the menu.php view, the menu links become additive. Now if I click the same the link it returns Code: index.php/costumes/animal/index.php/costumes/animal Example Menu Link: Code: <li class="subC"><a href="index.php/costumes/animal">Animal</a></li> Route: Code: [code]$route['costumes/(:any)'] = "costumes/product_lookup/$1";[/code] Home View: Code: <?php $this->load->view('header');?> Costumes Controller: Code: <?php Code: <?php $this->load->view('header');?> Again the menu link works fine when fired from the home view, but the url string duplicates itself on all subsequent view loads. Thanks in advance for any help on this matter.
[eluser]Chad Fulton[/eluser]
This is because you're using relative links instead of absolute links. If your URL does not begin with http:// or /, the browser interprets it as a link relative to the current page. So, if you're at http://www.example.com/index.php/costumes/porcupine, and you click Code: <a href="index.php/costumes/shark">Shark</a> If you click Code: <a href="http://www.example.com/index.php/costumes/shark">Shark</a> If you click Code: <a href="/index.php/costumes/shark">Shark</a> The way to do this in CodeIgniter in a portable way is: Code: <a href="<?php echo base_url(); ?>costumes/shark">Shark</a> Both of these will work (they will automatically append the "index.php" part as well). Note that you'll have to make sure you load the URL Helper (probably by autoloading).
[eluser]Unknown[/eluser]
Thanks for the quick reply Chad, this makes sense. I will go ahead and give it a try. |
Welcome Guest, Not a member yet? Register Sign In |