Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CodeIgniter's index.php to handle multiple hosts
<?php
// put this on application/hooks/
defined('BASEPATH') or exit('No direct script access allowed');
class Environment_hooks
{
/**
* Handles app hosts.
*
* @return void
*/
public function handleHosts()
{
// gets app instance
$CI =& get_instance();
// gets default app url
$app_url = getenv('APP_URL');
// checks ssl
$connection_type = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on" ? 'https' : 'http';
// checks to override app base url configuration
if ($app_url && $CI->config->item('base_url') !== $app_url) {
$CI->config->set_item('base_url', "$connection_type://$app_url");
}
}
}
<?php
// put this on application/config/
$hook['post_controller_constructor'][] = [
'class' => 'Environment_hooks',
'function' => 'handleHosts',
'filename' => 'Environment_hooks.php',
'filepath' => 'hooks',
'params' => []
];
<?php
// modify this part on index.php file, in your codeigniter root folder
$domain = php_sapi_name() === 'cli' ? [0 => 'api'] : explode('.', $_SERVER['HTTP_HOST']);
switch ($domain[0]) {
case 'api':
$application_folder = 'api';
break;
case 'admin':
$application_folder = 'admin';
break;
case 'bo':
$application_folder = 'bo';
break;
default:
$application_folder = 'application';
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment