Welcome Guest, Not a member yet? Register   Sign In
Why use base_url?
#1

[eluser]pbreit[/eluser]
I'm curious about how prudent it is to use base_url. Isn't it better to use relative URLs?
#2

[eluser]tonanbarbarian[/eluser]
base_url() and site_url() always produce absolute urls.
absolute urls are always safer
http://myhost.com/controller/method/param1 is absolute
/controller/method/param1 is not really absolute because it does not contain the hostname. it is not really relative either
../../images/home.png is relative and should NEVER be used, particularly with CI as it is the browser that determines what the path is not the server
so if you have the following page http://myhost.com/controller/method/ and you have a link to an image in it that is ../../images/home.png the absolute path that the browser calculates and requests of the server is http://myhost.com/images/home.png

However if you have parameters added onto the request
http://myhost.com/controller/method/param1 or http://myhost.com/controller/method/param1/param2 the browser will calculate the absolute path to the image as http://myhost.com/controller/images/home.png or http://myhost.com/controller/method/images/home.png

The only time you can put relative paths (for images) without issues is in CSS because the browser calculates the absolute path based on the location of the CSS file and not the original webpage.

absolute paths are always 100% safe, the path you request is the path you will get (if it exists)
#3

[eluser]pbreit[/eluser]
Yeah, that makes sense. I suppose I could set it to be something like "/path/to/project" without the domain?
#4

[eluser]Vheissu[/eluser]
In all of my Codeigniter projects I always declare a base href at the top of my pages that contains the base_url - that was, I don't have to throw a base_url in front of all of my links.




Theme © iAndrew 2016 - Forum software by © MyBB