Welcome Guest, Not a member yet? Register   Sign In
Dynamic $baseURL for multiple hosts
#1

(This post was last modified: 08-26-2020, 11:04 PM by jreklund.)

I am developing an app that will have multiple domains and subdomains pointed at it.  I would like to have CodeIgniter automatically detect the Base URL.

In Config/App.php it says this:

PHP Code:
    /*
    |--------------------------------------------------------------------------
    | Base Site URL
    |--------------------------------------------------------------------------
    |
    | URL to your CodeIgniter root. Typically this will be your base URL,
    | WITH a trailing slash:
    |
    |    http://example.com/
    |
    | If this is not set then CodeIgniter will try guess the protocol, domain
    | and path to your installation. However, you should always configure this
    | explicitly and never rely on auto-guessing, especially in production
    | environments.
    |
    */ 

I also have this line commented out in my .env file:
Code:
# app.baseURL = ''


I am receiving this error:

You have an empty or invalid base URL. The baseURL value must be set in Config\App.php, or through the .env file.

How can I dynamically set the Base Site URL to use for multiple sites pointing to the app?
Reply
#2

(08-26-2020, 08:04 PM)muncherelli Wrote: I am developing an app that will have multiple domains and subdomains pointed at it.  I would like to have CodeIgniter automatically detect the Base URL.

In Config/App.php it says this:

PHP Code:
    /*
    |--------------------------------------------------------------------------
    | Base Site URL
    |--------------------------------------------------------------------------
    |
    | URL to your CodeIgniter root. Typically this will be your base URL,
    | WITH a trailing slash:
    |
    |    http://example.com/
    |
    | If this is not set then CodeIgniter will try guess the protocol, domain
    | and path to your installation. However, you should always configure this
    | explicitly and never rely on auto-guessing, especially in production
    | environments.
    |
    */ 

I also have this line commented out in my .env file:
Code:
# app.baseURL = ''


I am receiving this error:

You have an empty or invalid base URL. The baseURL value must be set in Config\App.php, or through the .env file.

How can I dynamically set the Base Site URL to use for multiple sites pointing to the app?

My baseURL is set in the .ENV file, which if I remember rightly, takes precedence - worth checking that in the project root folder.
Reply
#3

(08-26-2020, 08:04 PM)muncherelli Wrote: I am developing an app that will have multiple domains and subdomains pointed at it.  I would like to have CodeIgniter automatically detect the Base URL.

In Config/App.php it says this:

PHP Code:
    /*
    |--------------------------------------------------------------------------
    | Base Site URL
    |--------------------------------------------------------------------------
    |
    | URL to your CodeIgniter root. Typically this will be your base URL,
    | WITH a trailing slash:
    |
    |    http://example.com/
    |
    | If this is not set then CodeIgniter will try guess the protocol, domain
    | and path to your installation. However, you should always configure this
    | explicitly and never rely on auto-guessing, especially in production
    | environments.
    |
    */ 

I also have this line commented out in my .env file:
Code:
# app.baseURL = ''


I am receiving this error:

You have an empty or invalid base URL. The baseURL value must be set in Config\App.php, or through the .env file.

How can I dynamically set the Base Site URL to use for multiple sites pointing to the app?
in config->App.php
 you must do some like this :

PHP Code:
class App extends BaseConfig
{    

    public 
$baseURL;

    public function 
__construct()
    {
        if (
$_SERVER['SERVER_NAME']== 'localhost') {

        
    $this->baseURL 'http://localhost:8080/';

        }else{

            
$this->baseURL 'https://yourwebsite/';
        }
    }

//other code 
Reply
#4

PHP Code:
   |--------------------------------------------------------------------------
   Base URL Initialization
   
|--------------------------------------------------------------------------
   Sets the base URL depending upon the ENVIRONMENT type.
   This is very similar to how things worked in CI3.
   */
  public function __construct()
  {

    if ('development' == $_ENV['CI_ENVIRONMENT'])
    {
      $this->baseURL 'http://url.test';
    }
    else
    {
      $allowed_domains = array('domain1.co.uk''domain1.com''domain1-preview.host.co.uk');
      $default_domain 'domain1.co.uk';

      if (in_array($_SERVER['HTTP_HOST'], $allowed_domainstrue))
      {
        $domain $_SERVER['HTTP_HOST'];
      }
      else
      {
        $domain $default_domain;
      }

      if (!empty($_SERVER['HTTPS']))
      {
        $this->baseURL 'https://' $domain;
      }
      else
      {
        $this->baseURL 'http://' $domain;
      }
    }
    parent::__construct();
  

I don't know if that function helps, but it enables me to use the same code for development, preview and production.

In CI4, I place it at the end of the App.php file in the Config folder.

I had a similar system with CI3, which was a little easier, but this seems to work nicely and isn't too expensive to run in terms of time.

I hope this is useful.

Don't forget, you might want to force HTTPS on the production server, but this code makes it easy for both development and production.
Reply
#5

'because index.php is always the first file encountered when browsing I define a constant which is passed to ./app/Config/App.php

https://github.com/John-Betong/ci4-stric.../index.php

The script was opied from a user on this forum and isI used it on numerous projects.

Tentatively tapped on a tablet Sad
Reply




Theme © iAndrew 2016 - Forum software by © MyBB