Reputation: 91
I am unable to load my own config file. How can I autoload or manual load this file this?
$psr4 = [
'Config' => APPPATH.'Config',
APP_NAMESPACE => APPPATH, // For custom namespace
'App' => APPPATH, // To ensure filters, etc still found,
'Tests\Support' => TESTPATH.'_support', // So custom migrations can run during testing
];
Upvotes: 4
Views: 4932
Reputation: 476
@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();
}
Upvotes: 3
Reputation: 8964
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.
Upvotes: 5