Setting Dynamic base URL in CodeIgniter 4.0.2

Viewed 15127

I am migrating my project from CodeIgniter 3 to CodeIgniter 4. I am getting confused for the new structure of the framework. So here's my problem,

I am setting my base url in App.php to:

protected $proj_root= "http://".$_SERVER['HTTP_HOST'];
protected $proj_root2  = str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
protected $mybase= $proj_root.$proj_root2;
public $baseURL =  $mybase;

But I am getting error like this:

Fatal error: Constant expression contains invalid operations in D:\xampp\htdocs\delivery_dashboard\app\Config\App.php on line 26

So literally I can only do this:

public $baseURL = "http://localhost/my_project/"

How can I set my base url dynamically using $_SERVER['HTTP_HOST'] or is there any workaround here?

Thanks for the help!

5 Answers

I dont know the exact solution but i shared my way of solution to you Go to app/Config/Constants.php

    $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https://'.$_SERVER['HTTP_HOST'] : 'http://'.$_SERVER['HTTP_HOST'];
    defined('BASE') || define('BASE',$protocol);

In app/Config/App.php

 public $baseURL    = BASE;

Hope this Helps

Go to app/Config/Constants.php and open it in your IDE. Then place this below code in the end of your file.

if ( (! empty($_SERVER['REQUEST_SCHEME']) && $_SERVER['REQUEST_SCHEME'] == 'https') ||
     (! empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ||
     (! empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443') ) {
    $protocole = 'https://';
} else {
    $protocole = 'http://';
}

$host = $_SERVER['HTTP_HOST'] . '/';
$project = explode('/', $_SERVER['REQUEST_URI']);
$baseurl = $protocole . $host . $project[1];
$myappBaseUrl = $baseurl.'/';
defined('BASESEURL') || define('BASESEURL',$myappBaseUrl);

and now open app/Config/App.php and replace This

public $baseURL = 'http://localhost:8080/';

with

public $baseURL = BASESEURL;

Improved answer of Boominathan

app/Config/Constants.php

$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https://'.$_SERVER['HTTP_HOST'] : 'http://'.$_SERVER['HTTP_HOST']."".str_replace(basename($_SERVER['SCRIPT_NAME']), "", $_SERVER['SCRIPT_NAME']);
defined('BASEURL') || define('BASEURL',$protocol);

In app/Config/App.php

public $baseURL = BASEURL;

Go to app/Config/Constants.php and add the following line of code at end of the file.

$base_url = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || isset($_ENV['FORCE_HTTPS'])) ? 'https' : 'http';
$base_url .= '://' . $_SERVER['HTTP_HOST'];
$base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '',   $_SERVER['SCRIPT_NAME']);
defined('BASESEURL') || define('BASESEURL', $base_url);

Then go to in app/Config/App.php, apply BASESEURL to $baseURL variable

 public $baseURL = BASESEURL;

Checked and working for dynamic baseURL and for PHP spark serve

defined('BASE_URL') OR define('BASE_URL', (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off'  ? 'https://'.$_SERVER['HTTP_HOST'] : 'http://') . $_SERVER['HTTP_HOST'] . dirname($_SERVER['SCRIPT_NAME']) . '/');
$config['base_url'] = BASE_URL;
Related