3

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!

Roshan
  • 786
  • 1
  • 9
  • 20

6 Answers6

13

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

Boominathan Elango
  • 1,156
  • 2
  • 7
  • 20
0

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;
General Grievance
  • 4,555
  • 31
  • 31
  • 45
0

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;
Joel Enanod Jr
  • 649
  • 9
  • 19
0

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;
Sam
  • 131
  • 1
  • 12
0

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;
khan ss
  • 1
  • 2
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 20 '21 at 03:21
0

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;
Murad Ali
  • 347
  • 2
  • 15