Welcome Guest, Not a member yet? Register   Sign In
CI portability
#1

[eluser]Unknown[/eluser]
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?
#2

[eluser]Rick Jolly[/eluser]
You can have a dynamic base_url. See here:http://ellislab.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.
Code:
// in index.php
define('IS_PRODUCTION', ($_SERVER['SERVER_NAME'] != 'localhost'));

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

[eluser]Seppo[/eluser]
About the base_url there are techniques to work with different hostings... the easiest thing is to do something like this
Code:
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.
#4

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

Code:
$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:

Code:
<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:

Code:
<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?
#5

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




Theme © iAndrew 2016 - Forum software by © MyBB