how to autoload a self made config file in codeigniter 4? how to autoload a self made config file in codeigniter 4? codeigniter codeigniter

how to autoload a self made config file in codeigniter 4?


Create your configuration file in the folder /application/Config and define the class in the file SomeConfig.php like this.

<?php namespace Config;class SomeConfig extends \CodeIgniter\Config\BaseConfig{    public $foo  = 'This is foo';    public $bar = 'This is bar';}

You "load" the class with this

$someConfig = new \Config\SomeConfig();

And you then use it with:

$fooMessage = $someConfig->foo;$barMessage = $someConfig->bar;

I don't have to do anything to /application/Config/Autoload.php

Don't confuse "autoloading" in CI v4 with the CI v3 feature of "Auto-loading Resources". They are altogether different things!

In v4 "autoloading" is about finding files based on the class namespace.

In v3 it is a feature that causes a class to be initialized (loaded) automatically when the framework is being started.


@DFriend is right, I just can't find anything related to autoload config. But we can add this in the initController function in the \App\Controllers\BaseController, like below:

public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)    {        // Do Not Edit This Line        parent::initController($request, $response, $logger);        //--------------------------------------------------------------------        // Preload any models, libraries, etc, here.        //--------------------------------------------------------------------        // E.g.: $this->session = \Config\Services::session();        $this->config = \Config\Services::YOUR_CONFIG_CLASS();    }