Welcome Guest, Not a member yet? Register   Sign In
Dynamic $baseURL for multiple hosts
#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


Messages In This Thread
RE: Dynamic $baseURL for multiple hosts - by Chroma - 08-27-2020, 04:10 AM



Theme © iAndrew 2016 - Forum software by © MyBB