Part of the EllisLab Network
   
 
Getting base url for CSS or image loading, modify index.php
Posted: 01 June 2009 10:14 PM   [ Ignore ]  
Lab Assistant
Avatar
RankRank
Total Posts:  139
Joined  05-23-2009

I modified my index.php and added this after the list of define()‘s that CI uses:

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?

 Signature 

Renowned Media: Website | Blog | Twitter | CodeVault

Our Products: RenownedStats | Royalty Free Music

Profile
 
 
Posted: 01 June 2009 10:31 PM   [ Ignore ]   [ # 1 ]  
Lab Technician
RankRankRankRank
Total Posts:  1242
Joined  04-19-2008

I don’t know if I understand what you mean but when you load, css, can’t you use,

<?php echo base_url() . 'css/styles.css'; ?>

The same thing goes with images.

 Signature 

PinoyTech - Web Development Blog

Profile
 
 
Posted: 01 June 2009 10:46 PM   [ Ignore ]   [ # 2 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  139
Joined  05-23-2009

It looks like base_url() is synonymous with str_replace(“index.php”, “”, $_SERVER[‘PHP_SELF’]), thanks.

 Signature 

Renowned Media: Website | Blog | Twitter | CodeVault

Our Products: RenownedStats | Royalty Free Music

Profile
 
 
Posted: 02 June 2009 01:54 AM   [ Ignore ]   [ # 3 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4839
Joined  07-14-2006

An alternative is to use the base tag.

<base href="<?php echo base_url() ?>">
<
link type="text/css" rel="stylesheet" href="css/styles.css">
Profile
 
 
Posted: 02 June 2009 02:41 AM   [ Ignore ]   [ # 4 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2690
Joined  05-18-2008
xwero - 02 June 2009 01:54 AM

An alternative is to use the base tag.

<base href="<?php echo base_url() ?>">
<
link type="text/css" rel="stylesheet" href="css/styles.css">

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?

 Signature 

I’m building a Project Management System for my 3rd year Uni project, Sign up to the beta
Track my progress | Post of the day: UI Designs
Get full auto complete support for CodeIgniter in Eclipse

Profile
 
 
Posted: 02 June 2009 02:58 AM   [ Ignore ]   [ # 5 ]  
Lab Technician
RankRankRankRank
Total Posts:  1242
Joined  04-19-2008

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.

 Signature 

PinoyTech - Web Development Blog

Profile
 
 
Posted: 02 June 2009 03:04 AM   [ Ignore ]   [ # 6 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4839
Joined  07-14-2006

base tag explanation :

The BASE element defines the document’s base URI for resolving relative URIs contained within the document.

so yes you only need it once.

Profile
 
 
Posted: 02 June 2009 05:50 AM   [ Ignore ]   [ # 7 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2690
Joined  05-18-2008

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

 Signature 

I’m building a Project Management System for my 3rd year Uni project, Sign up to the beta
Track my progress | Post of the day: UI Designs
Get full auto complete support for CodeIgniter in Eclipse

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120177 Total Logged-in Users: 55
Total Topics: 126307 Total Anonymous Users: 4
Total Replies: 664267 Total Guests: 494
Total Posts: 790574    
Members ( View Memberlist )