Reputation: 842
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!
Upvotes: 4
Views: 27334
Reputation: 418
This is will help you to add directory if any, and will also add trailing slash. This is very useful when you work on localhost as well as online i.e CI/CD.
app/Config/Constants.php
$base_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . dirname($_SERVER['SCRIPT_NAME']) . (substr(dirname($_SERVER['SCRIPT_NAME']), -1) == '/' ? '' : '/');
defined('BASE_URL') || define('BASE_URL', $base_url);
app/Config/App.php
public $baseURL = BASE_URL;
Upvotes: 1
Reputation: 1
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;
Upvotes: 0
Reputation: 141
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;
Upvotes: 0
Reputation: 669
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;
Upvotes: 0
Reputation: 11
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;
Upvotes: 1
Reputation: 1191
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
Upvotes: 13