Welcome Guest, Not a member yet? Register   Sign In
External JS with CI / PHP
#6

[eluser]Phil Sturgeon[/eluser]
That works fine until you move your application into a directory. You might think that doesn't happen very often but you'd be surprised how many developers still work on localhost without virtual hosts.

I actually generate my absolute paths in constants.php then use that value everywhere.

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);

Pass those to JS with:

Code:
var BASE_URI = "<?php echo BASE_URI; ?>";

then you are cooking with gas :-)


Messages In This Thread
External JS with CI / PHP - by El Forum - 03-17-2010, 05:57 AM
External JS with CI / PHP - by El Forum - 03-17-2010, 06:22 AM
External JS with CI / PHP - by El Forum - 03-17-2010, 06:57 AM
External JS with CI / PHP - by El Forum - 03-17-2010, 07:05 AM
External JS with CI / PHP - by El Forum - 03-17-2010, 07:13 AM
External JS with CI / PHP - by El Forum - 03-17-2010, 07:25 AM
External JS with CI / PHP - by El Forum - 03-17-2010, 07:30 AM
External JS with CI / PHP - by El Forum - 03-17-2010, 09:21 PM
External JS with CI / PHP - by El Forum - 03-18-2010, 12:30 AM



Theme © iAndrew 2016 - Forum software by © MyBB