Welcome Guest, Not a member yet? Register   Sign In
Asset handling in CodeIgniter with the BASE tag
#1

[eluser]Phil Sturgeon[/eluser]
I just posted an article on something I thought up a few days back: Asset handling in CodeIgniter with the BASE tag. I haven't seen this suggestion posted around the forums before so thought it might be worth mentioning.
#2

[eluser]steelaz[/eluser]
It's been mentioned a few times in this forum, but more people should be aware of <base> tag. When combined with automatic base_url (below) detection, you can copy website from http://127.0.0.1/dev/project to http://www.project.com without any modification to configuration files or templates.

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']);
#3

[eluser]Phil Sturgeon[/eluser]
Who when and where? I'll kill em. I'll kill em with a pointy shoe.

As for automatic base_url, that is the way I normally handle things but I take it one step further.

config/constants.php

Code:
/*
|--------------------------------------------------------------------------
| Docment root folders
|--------------------------------------------------------------------------
|
| These constants use existing location information to work out web root, etc.
|
*/

// Base URL (keeps this crazy sh*t out of the config.php
if(isset($_SERVER['HTTP_HOST']))
{
    $base_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 'https' : 'http';
    $base_url .= '://'. $_SERVER['HTTP_HOST'];
    $base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
    
    // Base URI (It's different to base URL)
    $base_uri = parse_url($base_url, PHP_URL_PATH);
    if(substr($base_uri, 0, 1) != '/') $base_uri = '/'.$base_uri;
    if(substr($base_uri, -1, 1) != '/') $base_uri .= '/';
}

else
{
    $base_url = 'http://localhost/';
    $base_uri = '/';
}

// Define these values to be used later on
define('BASE_URL', $base_url);
define('BASE_URI', $base_uri);
define('APPPATH_URI', BASE_URI.APPPATH);

// We dont need these variables any more
unset($base_uri, $base_url);

That way I can use these handy constants throughout the entire application.
#4

[eluser]kurucu[/eluser]
I mentioned it just the other day lol. Too far away to throw a pointy shoe at.
#5

[eluser]Phil Sturgeon[/eluser]
Just spotted that after looking over that post again. Both me and the author of the thread missed it. Nevermind, at least this explains why what and how.
#6

[eluser]BrianDHall[/eluser]
Great stuff, thanks!

As a spot of extra fun, I use:

Code:
if (strstr($_SERVER['HTTP_HOST'], 'localhost'))
{
    $GLOBALS['live'] = false;

}
else
{
    $GLOBALS['live'] = true;

}

A simple hack to set a variable that I use in my database, and possibly other places, to do things only if they are on my computer and not the live server.
#7

[eluser]Phil Sturgeon[/eluser]
Have you conidered using constants for this?

How to: Support multiple production environments in CodeIgniter
#8

[eluser]BrianDHall[/eluser]
Nice, much better - thanks Phil! I likey Smile




Theme © iAndrew 2016 - Forum software by © MyBB