Welcome Guest, Not a member yet? Register   Sign In
Setting baseURL in Registrar not working
#1

Hey guys,
the registrar does not seem to work (at least for me). I followed th documentation and set it up like so:
-------------------
Code:
<?php

namespace Config;

class Registrar{

  public function __construct(){
    defined('LOCATION') || define('LOCATION', $_SERVER['CI_ENVIRONMENT']);
    }

  public static function App(): array{
     
    $conf = [];
    $conf['baseURL'] = 'http://192.168.0.1:81/'; 
    if(LOCATION == 'staging') $conf['baseURL'] = 'http://mydomain.com/';

    return $conf;

    }
  }
---------------------

I always get the error 
Fatal error: Uncaught CodeIgniter\Exceptions\ConfigException: Config\App::$baseURL "/" is not a valid URL. in ...\vendor\codeigniter4\framework\system\HTTP\SiteURI.php:201

Autodiscovery is set to true.
Its only workig if a manually set the baseURL in App.php but the URL should be set dynamically based on the environment. CI3 was way more comfortable and dynamic to set up.
Anybody knows the problem?

Thanks and kind regards,
Daniel
Reply
#2

The baseURL you will set has to be listed in the allowedHostnames array.
Reply
#3

Hey michalsn,

i think you misunderstood.
I do not want to add additional domains/subdomain, i want to extend the App config to set up one single base url based on my current environment so i don't have to mess up the original config files with custom code.
Reply
#4

Unable to reproduce your error, here is a live example.

The code for app/Config/Registrar.php
PHP Code:
<?php

namespace App\Config;

class 
Registrar
{
    public static function 
Database(): array
    {
        return [
            
'default' => [
                
'database' => 'default.db',
                
'DBDriver' => 'SQLite3',
            ],
        ];
    }

    public static function 
App(): array
    {
        return [
            
'baseURL' => match(ENVIRONMENT) {
                
'staging' => 'http://mydomain.com/',
                default => 
'http://192.168.0.1:81/',
            },
            
'indexPage' => '',
            
'appTimezone' => 'America/New_York',
        ];
    }

Reply
#5

Hey grimpirate,

thanks for your reply.

In my case the problem is that the function 


PHP Code:
public static function App(): array {} 

is never invoked - but the Registrar constructor itself is executed.
Auto discovery is activated in Modules.php
Reply
#6

(This post was last modified: 05-21-2025, 11:49 AM by grimpirate.)

If it's never invoked how did you get the fatal error that is being thrown? The error is saying that you defined your baseURL as '/' in Config/App.php. Which is why the application fails. Your namespace is also wrong in your registrar, it's not namespace Config; it should be namespace App/Config;, which is perhaps another reason it isn't working.

EDIT:
Make the baseURL line in your Config/App.php the following:
PHP Code:
public string $baseURL 'http://192.168.0.1:81/'
And then make your Registrar
PHP Code:
<?php

namespace App\Config;

class 
Registrar
{
    public static function App(): array
    {
        if(ENVIRONMENT == 'staging')
                return ['baseURL' => 'http://mydomain.com/'];
        return [];
    }

Reply
#7

The fatal error results by not setting the baseUrl in App.php because i'd like to only set it inside Registrar. Seems like it _must_ be initially set in App.php anyway.

After further investigation i've found out that the method App() is not listed as callable inside 

"vendor\codeigniter4\framework\system\Config\BaseConfig.php" :

PHP Code:
// ignore non-applicable registrars
if (! method_exists($callable$shortName)) {
  continue; // @codeCoverageIgnore
  

--> lists the following methods as callable:

Exceptions
ContentSecurityPolicy
Cache
UserAgents
Cookie
Toolbar
Logger
Auth
Routing
Filters
AuthRoutes

So that seems like the reason why it never gets called and the constructor of the Registrar does. 
Don't know why it is working in your setup, please check by adding a "die()" inside the App() method of the Registrar.
Reply
#8

Note:

Values from .env always take priority over Registrars.

Make sure that your baseURL is not set in your .env file.
What did you Try? What did you Get? What did you Expect?

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

Yes, thats already mentioned in the docs. There is no value set in .env.
Looks like App can not be extended by an implicit registrar. This ist just possible with the listed callables.


The docs say:

Quote:This feature is implemented in the CodeIgniter\Config\BaseConfig class. So it will not work with a few files in the app/Config folder that do not extends the class.

App Configuration is extending BaseConfig so it should be callable. Any plans to extend/fix this in future releases?
Thanks
Reply
#10

(05-21-2025, 11:14 PM)petewulf1 Wrote: App Configuration is extending BaseConfig so it should be callable. Any plans to extend/fix this in future releases?

There's nothing to fix here - everything is working correctly, as already mentioned by other users.

To confirm, I ran a quick test:

Code:
composer create-project codeigniter4/appstarter ci461


Then I copied your code from the initial post and added a call to site_url() in the welcome_message.php view file.

I didn't encounter any issues. And the correct address was displayed: http://192.168.0.1:81/index.php

The same after setting:

Code:
CI_ENVIRONMENT = staging

Of course, I had to create a dedicated file in the app/Config/Boot/staging.php, but after that, again, the address was displayed correctly: http://mydomain.com/index.php

I simply can't reproduce the error you're describing on a fresh install.

If you have any additional details that could help us reproduce the behavior you're experiencing, please share them - otherwise, there's not much more we can do on our end.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB