Welcome Guest, Not a member yet? Register   Sign In
Getting base url for CSS or image loading, modify index.php
#1

[eluser]renownedmedia[/eluser]
I modified my index.php and added this after the list of define()'s that CI uses:

Code:
define('SERVERPATH', str_replace("index.php", "", $_SERVER['PHP_SELF']));

Based on the installation directory, SERVERPATH will equal the following:

http://localhost/test/index.php -> /test/
http://www.example.com/index.php -> /

Which is useful for me and probably a lot of users who do development on a localhost subdirectory and later upload to a real server.

My question is if there is a better way to do this, like if CI has this functionality built in without modifying a core file?
#2

[eluser]Thorpe Obazee[/eluser]
I don't know if I understand what you mean but when you load, css, can't you use,
Code:
<?php echo base_url() . 'css/styles.css'; ?>
The same thing goes with images.
#3

[eluser]renownedmedia[/eluser]
It looks like base_url() is synonymous with str_replace("index.php", "", $_SERVER['PHP_SELF']), thanks.
#4

[eluser]xwero[/eluser]
An alternative is to use the base tag.
Code:
<base href="<?php echo base_url() ?>">
<link type="text/css" rel="stylesheet" href="css/styles.css">
#5

[eluser]Dam1an[/eluser]
[quote author="xwero" date="1243943649"]An alternative is to use the base tag.
Code:
<base href="<?php echo base_url() ?>">
<link type="text/css" rel="stylesheet" href="css/styles.css">
[/quote]

I've never seen it done like that, do you only need to specify the base once and it as used for all your css/js?
#6

[eluser]Thorpe Obazee[/eluser]
I think it's used by the currently loaded page as a base just like base_url() in CI.

Anyway, I believe that's the first time, I've seen the html <base> tag.
#7

[eluser]xwero[/eluser]
base tag explanation :
Quote:The BASE element defines the document's base URI for resolving relative URIs contained within the document.
so yes you only need it once.
#8

[eluser]Dam1an[/eluser]
Wow, I was going through my RSS and thought it was a coincidence I found out about the base tag twice in one day (link here) and then I realised it was bargainphs blog lol




Theme © iAndrew 2016 - Forum software by © MyBB