Welcome Guest, Not a member yet? Register   Sign In
How to set dynamic url in Codeigniter 4?
#1
Question 
(This post was last modified: 03-14-2023, 11:37 AM by cijagani. Edit Reason: formating )

Q : how to set dynamic url in Codeigniter 4 ?

let's assume that  i have following informations / configurations are ready 

one domain : ci4multitenant.local  pointing to /var/www/html/myproject/public
i have 2 subdomains branch1.ci4multitenant.local  pointing to /var/www/html/myproject/public
branch2.ci4multitenant.local pointing to /var/www/html/myproject/public

configuration of .env file is here https://prnt.sc/2xsEA3pgBV7B

when i open ci4multitenant.local in browser debug bar works fine

but when i visit branch1.ci4multitenent.local or branch2.multitenant.local debugbar is not working / showing.

is there any way to set baseURL dynamically?
Reply
#2

(03-14-2023, 11:34 AM)cijagani Wrote: Q : how to set dynamic url in Codeigniter 4 ?

let's assume that  i have following informations / configurations are ready 

one domain : ci4multitenant.local  pointing to /var/www/html/myproject/public
i have 2 subdomains branch1.ci4multitenant.local  pointing to /var/www/html/myproject/public
branch2.ci4multitenant.local pointing to /var/www/html/myproject/public

configuration of .env file is here https://prnt.sc/2xsEA3pgBV7B

when i open ci4multitenant.local in browser debug bar works fine

but when i visit branch1.ci4multitenent.local or branch2.multitenant.local debugbar is not working / showing.

is there any way to set baseURL dynamically?

use virtual host
Reply
#3

Setting Dynamic base URL in CodeIgniter 4.0.2
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(This post was last modified: 03-15-2023, 01:49 AM by cijagani.)

(03-15-2023, 12:18 AM)InsiteFX Wrote: Setting Dynamic base URL in CodeIgniter 4.0.2

@InsiteFX thanks for the reply. i followed it. but it only works for maindomain. here are some screenshots.


https://prnt.sc/s1UtF77e9R9B

debugbar is not loading when i access subdomains
https://prnt.sc/7yepG_H-KPLa

https://prnt.sc/i8TS1H0oqvHN

it seems some issues in debugbar logic

   https://github.com/codeigniter4/CodeIgni...11C6-L11C6

i created Filter to set dynamic base url

Filter name : VerifyTenant
here is snippet and screenshot of Filter.php
Code:
public function before(RequestInterface $request, $arguments = null){
        $config = config('App');
        $subdomain = $request->header('host')->getValue()."/";
        $config->baseURL = $subdomain;
        // assume that all the subdomains are valid
        $config->allowedHostnames = [$subdomain];
        $uri = service('uri');
        // set dynamice base url here
        $uri->setBaseURL($subdomain);
    }

it seems working fine. but debugbar is not loading when you access subdomains.
Reply
#5

(03-14-2023, 08:41 PM)startup Wrote:
(03-14-2023, 11:34 AM)cijagani Wrote: Q : how to set dynamic url in Codeigniter 4 ?

let's assume that  i have following informations / configurations are ready 

one domain : ci4multitenant.local  pointing to /var/www/html/myproject/public
i have 2 subdomains branch1.ci4multitenant.local  pointing to /var/www/html/myproject/public
branch2.ci4multitenant.local pointing to /var/www/html/myproject/public

configuration of .env file is here https://prnt.sc/2xsEA3pgBV7B

when i open ci4multitenant.local in browser debug bar works fine

but when i visit branch1.ci4multitenent.local or branch2.multitenant.local debugbar is not working / showing.

is there any way to set baseURL dynamically?

use virtual host

already created
Reply
#6
Lightbulb 
(This post was last modified: 03-16-2023, 11:01 AM by cijagani.)

Finally i managed it by using Registrar.php

would like to share code here. Smile


PHP Code:
<?php
namespace Config;
use 
ReflectionClass;
use 
ReflectionProperty;

class 
Registrar{
    public static function setDynamicBaseUrl(){
        $ref  = new ReflectionClass('Config\App');
        $props $ref->getDefaultProperties();
        $baseUrl Services::request()->header('host')->getValue()."/";
        $props['baseURL'] = $baseUrl;
        service('uri')->setBaseURL($baseUrl);
    }
}
Registrar::setDynamicBaseUrl(); 
Reply
#7

I only use virtual host in ci 4.3.2 working perfect
Reply
#8

(03-16-2023, 08:16 PM)startup Wrote: I only use virtual host in ci 4.3.2 working perfect
i am using multitenant concept with multi db.
Reply
#9

@cijagani If you modify the URI object, check the all properties.
See https://github.com/codeigniter4/CodeIgni...1441049732
Reply
#10

@kenjis thanks for informing me.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB