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

i2loujxw  于 2022-12-07  发布在  其他
关注(0)|答案(3)|浏览(181)

我正在尝试使用自定义配置文件从数据库中加载一些常规设置,方法是从表中获取值。下面是我的配置文件

<?php

namespace Config;

use CodeIgniter\Config\BaseConfig;
use Config\Database;

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 App\Controllers;
use App\Controllers\BaseController;
use Config\MyConfig;

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>
jhiyze9q

jhiyze9q1#

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

参考:

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

$myconfig = new MyConfig;

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

whhtz7ly2#

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

oyxsuwqo

oyxsuwqo3#

通过使用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; ?>

相关问题