CodeIgniter Forums
How change Config (base_url) on a Controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: How change Config (base_url) on a Controller (/showthread.php?tid=81878)



How change Config (base_url) on a Controller - lelolenda - 05-13-2022

I want build a website with subdomains, to a multi-language purpose, and that solution can help me.

Code eg.

Code:
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        //HERE something like that (but that dont work, of course)
        $config['base_url'] = 'https://en.test.com';

     
        // Do Not Edit This Line
        parent::initController($request, $response, $logger);

        // Preload any models, libraries, etc, here.

        // E.g.: $this->session = \Config\Services::session();
    }



RE: How change Config (base_url) on a Controller - InsiteFX - 05-14-2022

@includebeer, Has a very nice tutorial on how to do this multilingual website.

Creating a multilingual website with CodeIgniter 4

He also has another tutorial for creating a website.


RE: How change Config (base_url) on a Controller - pvt - 05-14-2022

You can store info in database then use code to detect valid domain.
Idea:
In admin controller, create CRUD to manager domain list:

Code:
//DATABASE TABLE domainList:
[domain, isActive, ... ]
1. abc.mydomain.com - 1
2. xyz.mydomain.com - 1
3. other.mydomain.com - 0

Include to header file:

Code:
//check valid domain, $currentDomain = $_SERVER['SERVER_NAME'];
// use where clause in DB BUILDER, ex: where("domain LIKE '%mydomain.com'"); where("isActive", 1); 
if(!$currentDomain in DB TABLE)
{
die ("Invalid domain");
}
else
{
$base_url = get_base_url();
//if domain in db - $base_url = current domain, else $base_url = default_url (get from file config)
}