我在 Config 文件夹中创建了一个自定义配置文件。配置/Elearning_config.php。现在我想使用Codeigniter v4 的Autoload.php 加载它。这个怎么做?
<?php namespace Config;
use CodeIgniter\Config\BaseConfig;
class Elearning_config extends BaseConfig
{
public $theme = 'theme/etreeks/';
}
我在 Config 文件夹中创建了一个自定义配置文件。配置/Elearning_config.php。现在我想使用Codeigniter v4 的Autoload.php 加载它。这个怎么做?
<?php namespace Config;
use CodeIgniter\Config\BaseConfig;
class Elearning_config extends BaseConfig
{
public $theme = 'theme/etreeks/';
}
我无法使用Autoload.php加载此配置文件。但是我发现了一个自动加载它的过程。
在BaseController 中写下他的行
$this->Elearning_config = new \Config\Elearning_config();
并从控制器获取
echo $this->Elearning_config->theme;
我认为这不是自动加载它的正确过程。但这是有效的
所有文件都通过 psr4 检查行加载到 Config 文件夹中,没有 $psr4 数组的第三个值
$psr4 = [
'App' => APPPATH, // To ensure filters, etc still found,
APP_NAMESPACE => APPPATH, // For custom namespace
'Config' => APPPATH . 'Config', // check this line if not loaded
];
它将自动加载您的配置现在在任何控制器上使用此文件和变量,如下所示
<?php namespace App\Controllers;
class Home extends BaseController
{
public function index()
{
$Elearning_config = new \Config\Elearning_config();
echo $Elearning_config->theme;// exit;
return view('welcome_message');
}
}
如果您有任何疑问,请发表评论