CodeIgniter Forums
Use base_url for all internal links? - 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: Use base_url for all internal links? (/showthread.php?tid=44061)

Pages: 1 2


Use base_url for all internal links? - El Forum - 08-01-2011

[eluser]GeXus[/eluser]
I have an app that has a dev, staging and production environment. Each one, has a different URL structure.

Do you recommend using base_url() for all links? This works, but is there a better way?

Thanks!


Use base_url for all internal links? - El Forum - 08-01-2011

[eluser]tomcode[/eluser]
No, it's site_url() for internal links, base_url() for assets, read the User Guide


Use base_url for all internal links? - El Forum - 08-01-2011

[eluser]Aken[/eluser]
Honestly, they're both interchangeable if you've removed index.php from your URL structure. The only difference then becomes that base_url() takes no parameters, whereas you can input additional URL segments into site_url() for output.
Code:
// These output the same if you've removed index.php:

echo base_url() . 'controller/method';
echo site_url() . 'controller/method';
echo site_url('controller/method');



Use base_url for all internal links? - El Forum - 08-01-2011

[eluser]tomcode[/eluser]
I disagree, using base_url() makes it impossible to work without rewriting the URL, which is be very convenient for developing and testing or special case scenarios.


Use base_url for all internal links? - El Forum - 08-01-2011

[eluser]GeXus[/eluser]
Ok, so I'm just using this in my views, so for example:

Code:
<?=base_url()?>assets/img/img.jpg
<?=base_url()?>controller/method

Seems to work fine...


Use base_url for all internal links? - El Forum - 08-02-2011

[eluser]LuckyFella73[/eluser]
@ GeXus

Quote:I disagree, using base_url() makes it impossible to work without rewriting the URL, which is be very convenient for developing and testing or special case scenarios.

Thats a good point I have never thought about (though I should have Wink ).
In this manner this would be right:

Code:
<?php echo base_url();?>assets/img/img.jpg
<?php echo site_url();?>controller/method

Like Tomcode said: basically both are working but in case you can't use mod_rewrite
for any reason you would have to change all base_url()s pointing to a controller/method.


Use base_url for all internal links? - El Forum - 08-02-2011

[eluser]GeXus[/eluser]
Ok, well thanks Tomcode and Lucky.

Now, should the site_url be used in the same fashion? or should it be used like in the User Guide:

Code:
<?=site_url('controller/method');?>



Use base_url for all internal links? - El Forum - 08-02-2011

[eluser]LuckyFella73[/eluser]
Like posted before:
Code:
# use base_url() when pointing to an asset (image, css-file, js-file etc.)

// for example
echo '<img src="'.base_url().'assets/img/image.jpg" width="100%" height="100%" alt="txt" />';


# use site_url() when pointing to a controller/method

// for example
echo '<a href="'.site_url().'controller/method">Link</a>"';



Use base_url for all internal links? - El Forum - 08-02-2011

[eluser]GeXus[/eluser]
Ok, thanks!


Use base_url for all internal links? - El Forum - 08-02-2011

[eluser]skunkbad[/eluser]
You might also extend the url helper to cover situations like:

1) If you are on a standard page but need a link to an HTTPS page.

2) If you are on an HTTPS page and need a link to a standard page.

3) If you are on a page that might be standard or HTTPS, and need links to change accordingly.

In my own extended url helper, I have the following functions:

1) secure_base_url()

2) secure_site_url()

3) if_secure_base_url()

4) current_url() // output for std or HTTPS

5) secure_anchor()