CodeIgniter Forums

Full Version: URI confusion
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[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
. Each click which follows on subsequent view loads continues this, rendering my links useless. I have included my code below. Any help/insight would be greatly appreciated.

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:
&lt;?php $this->load->view('header');?&gt;
    <div id="mainContent">
    <div id="rightCol">
        <img src="&lt;?=base_url()?&gt;assets/img/edhardyIntimates_logo.jpg" class="ad" />
    </div>
    <div id="home">
        <img src="&lt;?=base_url()?&gt;assets/img/costumes.png" id="mainAd" />
    </div>
        &lt;?php $this->load->view('menu');?&gt;
    </div>&lt;!--End of maincontainer--&gt;
    <br class="clearfloat" />
&lt;?php $this->load->view('footer');?&gt;

Costumes Controller:
Code:
&lt;?php

class Costumes extends Controller {

    function Costumes()
    {
        parent::Controller();
        $this->load->model('Content');
        $subcat = $this->uri->rsegment(3);
    }
    
    function index()
    {
        
    }
    
    function product_lookup($subcat)
    {
        $category = $this->uri->rsegment(1);
        $data['query'] = $this->Content->product_by_subcat($category, $subcat);
        $data['category'] = $category;
        $data['subcat'] = $subcat;
        $this->load->view('product', $data);
    }
}
Product View:
Code:
&lt;?php $this->load->view('header');?&gt;
    <div id="mainContent">
        <div id="display">
            <h3>Product Title</h3>
            <img src="img/detail.jpg" class="detail" alt="detail image" />
            <img src="img/detail.jpg" class="detail" alt="detail image" />
            <p>
            Some Costume item # 123456
            </p>
        </div>&lt;!--End of display--&gt;
        <div id="thumbnails">
            <h2>&lt;?=$category?&gt; &raquo; &lt;?=$subcat?&gt;</h2>
            <span class="pagination">&lt; 1 2 3 &gt;</span>                
            &lt;?php foreach($query as $row) :?&gt;
            <img >thumb?&gt;" class="thumb" alt="thumbnail"/>
            &lt;?php endforeach?&gt;
            <span class="pagination">&lt; 1 2 3 &gt;</span>
        </div>&lt;!--End of thumbnails--&gt;
        &lt;?php $this->load->view('menu');?&gt;
    </div>&lt;!--End of maincontainer--&gt;
    <br class="clearfloat" />
&lt;?php $this->load->view('footer');?&gt;

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.

El Forum

[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>
, you'll end up at http://www.example.com/index.php/costume...umes/shark

If you click
Code:
<a href="http://www.example.com/index.php/costumes/shark">Shark</a>
, then you'll end up at http://www.example.com/index.php/costumes/shark.

If you click
Code:
<a href="/index.php/costumes/shark">Shark</a>
, then the browser will look for the root of your web directory (http://www.example.com), and go from there.

The way to do this in CodeIgniter in a portable way is:

Code:
<a href="&lt;?php echo base_url(); ?&gt;costumes/shark">Shark</a>
OR
<a href="&lt;?php echo site_url('costumes/shark'); ?&gt;">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).

El Forum

[eluser]Unknown[/eluser]
Thanks for the quick reply Chad, this makes sense. I will go ahead and give it a try.