Welcome Guest, Not a member yet? Register   Sign In
Purpose of anchor() and $baseURL vs. relative URLs
#1

[eluser]MMacdonald[/eluser]
Consider the following directory structure where page.php would be accessed at http://www.domain.com/subfolder/application/

Code:
htdocs
|
subfolder
|
application
|
system index.php global_css global_media
|                |
page.php         style.css

In my previous designs before I used a framework, I always kept my code portable by only using relative URLs. So to use the style sheet in page.php I would include the following :

Code:
<head>
<style type="text/css">
@import url(../global_css/style.css);
</style>
<head>

This technique served me well because it meant I could move my application folder anywhere and the site wouldn't break. My question is : "what is the rationale behind $baseURL and the anchor() function in CI?". I now use the following (clearly the above directory structure no longer applies) :

Code:
<head>
<style type="text/css">
@import url(<?=$baseURL?>styles/style.css);
</style>
<head>

What benefit do I get from using $baseURL or the anchor() function? I've used CSS as an example here but clearly this question applies to all cases where a link is required. In what way is a clever routine to insert absolute URLs an improvement over relative URLs? I now have to edit a config file every time I move the code and this feels like a step backwards.

Kind regards,
Mark Macdonald
#2

[eluser]GSV Sleeper Service[/eluser]
code igniter urls tend to look like
Code:
http://www.foo.com/controller/method/any/number/of/extra/parameters
if you didn't use absolute urls your web server would have a hell of a time searching for images / javascript etc.
personally I use the <base> tag, then you can use relative urls in your views, but they'll be relative to the path set in the <base> tag.
#3

[eluser]wiredesignz[/eluser]
The only benefit of using relative URL's in client side code (HTML or CSS) is to reduce the overall transmitted file size and maybe reduce the amount of typing you need to do.

The browser still has to prefix the source http://domain when resolving the URL properly.

Whereas base_url() and anchor() provide the full URL dynamically and help reduce typing.




Theme © iAndrew 2016 - Forum software by © MyBB