Welcome Guest, Not a member yet? Register   Sign In
muti domain support.
#1

Hi,

In CI3 , it was:

    if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") {
    $ssl_set = "s";
    } else {
    $ssl_set = "";
    }
    */

    //$config['base_url'] = 'http'.$ssl_set.'://'.$_SERVER['HTTP_HOST'];

in CI4, if statements do not seem to work and give a 500 error.
What is the best way to use/detect multiple subdomains/domain ?

    public $baseURL = ??


The SaaS application that I am trying to port from CI3 works like

subdomain1.domain.com
subdomain2.domain.com ..   -- can also be mapped to domain2.com 



I am checking how to achieve the same in CI4 ?


Thanks,
Reply
#2

Config classes are just that - classes. That means that setting class variables needs to be done in the class constructor.
Reply
#3

we faced the same problem - our solution/workaround was to implement "array support for baseURL"

Here is our PatchDiff

how it works:
detection function in common.php

replace all baseURL usage by detection function in:
system/Helpers/url_helper.php:107
system/HTTP/IncommingRequest.php:173
system/HTTP/Response.php:795

hope it can help you
Reply
#4

here is the code for people interested in multi url support:
PHP Code:
app\Common.php
function detectBaseURL() : string
{
  // cache url
  static $finalURL null;
  if ($finalURL !== null) {
     return $finalURL;
  }
  $URLs config(App::class)->baseURL;
  // multi url support
  if (is_array($URLs))
  {
     $host = [$_SERVER['SERVER_NAME'], $_SERVER['SERVER_ADDR']];
     foreach ($URLs as $url
     {
        $schemaLength strpos($url'//') + 2;
        $hostLength  strpos($url'/'$schemaLength);
        $Host        substr($url$schemaLength$hostLength $schemaLength);
        if (in_array($Host$host))
        {
           $finalURL $url;
           break;
        }
     }
     // use first as backup
     if ($finalURL === null)
     {
        $finalURL $URLs[0];
     }
  } elseif (! is_string($URLs))
  {
     // TODO : handle invalid baseurl
     $finalURL = (string) $URLs;
  } else {
     $finalURL $URLs;
  }
  return $finalURL;

call
PHP Code:
detectBaseURL 
in app\Config\App.php constructor
PHP Code:
public function __construct()
{
   parent::__construct();
   $this->baseURL detectBaseURL();

if you want to separate the url strictly you could store a url id in the app config and check this in your controller ( or directly in your routes config )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB