Part of the EllisLab Network
   
 
CI portability
Posted: 10 May 2008 05:41 PM   [ Ignore ]  
Summer Student
Total Posts:  2
Joined  05-10-2008

I’m a CI newbie and was just wandering around the excellent documentation and trying to do a simple web app, but I have two questions that let me think that CI is not as portable as it should be, or maybe I just don’t know yet how to overcome this issues:

base_url: The use of a hardcoded url leads to the need of change that parameter every time the web app is changed from domain or ip. This means the web app can’t be used from different network segments at the same time even if the server has multiple ip’s. I just want to change my app from ip or domain without the need to change anything.

.htaccess: I’m not familiarized with hosting, but as far as I know the default installation of Apache in Red Hat and Debian disbales this kind of file with the directive “AllowOverride None”, wich means my pages can’t access any image, css or javascript file, wich makes them unusable. I don’t want to change anything in the server just to run my app, beacuse most of the times we can’t access the server configuration.

Is there any workaround for this issues or CI is just aimed for traditional web hosting where your page is always accessed with only one domain name and the server allows the use of .htaccess files?

Profile
 
 
Posted: 10 May 2008 05:54 PM   [ Ignore ]   [ # 1 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  718
Joined  02-05-2007

You can have a dynamic base_url. See here:http://codeigniter.com/forums/viewthread/60181/

htaccess is optional, but without it you’re stuck with index.php in all your urls. Honestly though, you’d have a hard time finding a (edit: *nix) host that doesn’t support .htaccess.

CI is as portable as you make it. For example, I define a “PRODUCTION” constant in the index.php bootstrap file. That way I can conditionally change my database configuration and other things automatically.

// in index.php
define('IS_PRODUCTION', ($_SERVER['SERVER_NAME'] != 'localhost'));

// in database.php config
$active_group = (IS_PRODUCTION) ? $db['production'] : $db['default'];

 Signature 

“I am the terror that flaps in the night”

Profile
 
 
Posted: 10 May 2008 05:56 PM   [ Ignore ]   [ # 2 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  732
Joined  10-18-2006

About the base_url there are techniques to work with different hostings… the easiest thing is to do something like this

switch ($_SERVER['SERVER_NAME'])
{
    
case 'localhost':
        
$config['base_url'] = 'http://localhost/codeigniter/';
        break;

    case
'...':
        
$config['base_url'] = 'http://other-domain/';
        break;

    default:
        
$config['base_url'] = 'http://something-else/';
        break;
}

About the .htaccess CI does not require it to work. As a matter of fact, I have a CI installation under IIS which does not allow .htaccess (it support some other things similar) but it is not a requirement.
The only used for .htaccess is to remove “index.php/” from your URLs when using CodeIgniter.

I don’t understand why are you saying that your “pages can’t access any image, css or javascript file” or why do you think you need to change server settings… You can include files (as a matter of fact, you have helper functions to do so), and you do not have to change server settings.

 Signature 

Once in a while I remember I use Twitter

Profile
 
 
Posted: 10 May 2008 07:37 PM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  2
Joined  05-10-2008

Very well. The link from Rick Jolly was very useful and I added the following to config.php:

$config['base_url'] = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
$config['base_url'] .= "://".$_SERVER['HTTP_HOST'];
$config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);

Now I can use my app if I type localhost or my ip or my machine name, I supose my app will work if I open the port in my router and type my public ip. It would be nice if Code Igniter were putting this lines by default, of course with the option to modify it.

For the .htaccess, the reason I tought it was imperative to use it is because if I put something like this:

<img src='images/arrow.jpg'/>

Always the full url was something like “http://localhost/ci/index.php/images/arrow.jpg” and the image was never displayed.
But now that I can use de base_url() function I can put it this way as php:

<img src='".base_url()."images/arrow.jpg'/>

This solves the problem.
I don’t know if the file .htaccess is used for something else.


EDIT: But what about css? I can achieve the same efect using php directives inside the css file but it makes it dirty to have php functions every time an image needs to be displayed. Is there a way to display images from css files without the need to use php?

Profile
 
 
Posted: 11 May 2008 09:36 PM   [ Ignore ]   [ # 4 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  258
Joined  10-11-2007

If you plan to always have the images folder in the root, you can use /images/arrow.jpg, or use relative links ../../images etc.

 Signature 

CI Bookmarks.

BarrettNewton.com

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 719, on June 06, 2008 10:16 AM
Total Registered Members: 62793 Total Logged-in Users: 23
Total Topics: 77480 Total Anonymous Users: 0
Total Replies: 418201 Total Guests: 152
Total Posts: 495681    
Members ( View Memberlist )
Newest Members:  newbie boymarkrichesongerdyPorscheescapeexpyAnn BaileyTy BexDamien2k8cibbuser