• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use base_url for all internal links?

#1
[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!

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

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

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

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

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

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

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

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

#10
[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()


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.