从数据库CodeIgniter 4获取常规配置设置



我正试图通过从表中获取值,使用自定义配置文件从DB中加载一些常规设置。下面id我的配置文件

<?php
namespace Config;
use CodeIgniterConfigBaseConfig;
use ConfigDatabase;
class MyConfig extends BaseConfig {
public function __construct() {
parent::__construct();
$db = Database::connect();
$get_configuration = $db->query('SELECT * FROM configuration');
$this->configuration = $get_configuration->getResult();
$db->close();
}
}

我正试图从控制器中的配置文件中获取一个特定的值。var_dump((表示我正在获取值,但无法将其传递给视图。

<?php
namespace AppControllers;
use AppControllersBaseController;
use ConfigMyConfig;
class Home extends BaseController {
public function index() {
$myconfig = new MyConfig;
//echo var_dump($myconfig);
$data = array('value' => $this->data['configuration'][1]->value);
$this->data['page_title'] = 'Dashboard';
return view('common/home', $this->data);
//return view('common/home');
}
}

视图在下面,我正试图获得网站名称

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?php echo ($data['value'][2]); ?> | <?php echo $page_title; ?></title>
<!-- Google Font: Source Sans Pro -->
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
<!-- Font Awesome -->
<link rel="stylesheet" href="assets/plugins/fontawesome-free/css/all.min.css">
<!-- Theme style -->
<link rel="stylesheet" href="assets/dist/css/adminlte.min.css">
</head>
<body class="hold-transition sidebar-mini">
<div class="wrapper">
<?= $this->include('layouts/header'); ?>

<?= $this->include('layouts/sidebar'); ?>

<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h1><?php echo $page_title; ?></h1>
</div>
<div class="col-sm-6">
<ol class="breadcrumb float-sm-right">
<li class="breadcrumb-item"><a href="#">Home</a></li>
<li class="breadcrumb-item active"><?php echo $page_title; ?></li>
</ol>
</div>
</div>
</div><!-- /.container-fluid -->
</section>
<!-- Main content -->
<section class="content">
<!-- Default box -->
<div class="card">
<div class="card-header">
<h3 class="card-title"><?php echo $page_title; ?></h3>
</div>
<div class="card-body">
<?= $this->renderSection('content'); ?>
</div>
<!-- /.card-body -->
<div class="card-footer">
Footer
</div>
<!-- /.card-footer-->
</div>
<!-- /.card -->
</section>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
<?= $this->include('layouts/footer'); ?>
<!-- Control Sidebar -->
<aside class="control-sidebar control-sidebar-dark">
<!-- Control sidebar content goes here -->
</aside>
<!-- /.control-sidebar -->
</div>
<!-- jQuery -->
<script src="assets/plugins/jquery/jquery.min.js"></script>
<!-- Bootstrap 4 -->
<script src="assets/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- AdminLTE App -->
<script src="assets/dist/js/adminlte.min.js"></script>  
</body>
</html>

您无法将配置值传递到视图,因为您声明了configuration对象$myconfig = new MyConfig;,但在控制器的index方法体中始终未使用。

参考:

使用配置文件

所有配置对象属性都是公共的,因此您可以像访问任何其他属性一样访问设置:

$myconfig = new MyConfig;
// Access settings as object properties.
$settings = $myconfig->configuration;

我想你可能在追求Registrars(https://codeigniter4.github.io/CodeIgniter4/general/configuration.html#registrars)使您能够使用自定义配置设置。这样做的好处是,您应该能够使用默认的Codeigniter调用在代码中的任何位置调用config('MyConfig')->setting_name

使用Anthony和Steven的建议。我在我的基本控制器公共函数中放置了以下代码,并且我能够很好地引用这些值:

$myconfig = new MyConfig;
$settings = $myconfig->configuration;
$this->data['language']     =  $settings[0]->value;
$this->data['businessname'] =  $settings[1]->value;
$this->data['email']=  $settings[2]->value;
$this->data['domain']       =  $settings[3]->value;
$this->data['systemurl']    =  $settings[4]->value;

然后在我看来,我简单的呼叫设置,我想要:

<?php echo $businessname; ?>

相关内容

最新更新