CodeIgniter's index.php to handle multiple hosts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// put this on application/config/ | |
$hook['post_controller_constructor'][] = [ | |
'class' => 'Environment_hooks', | |
'function' => 'handleHosts', | |
'filename' => 'Environment_hooks.php', | |
'filepath' => 'hooks', | |
'params' => [] | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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