I am trying to load some general settings from the DB using a custom config file by getting the values from a table. Below id my config file
<?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();
}
}
I am trying to get a specific value from my config file within the controller. A var_dump() indicate that I was getting value but not able to pass it to the view.
<?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');
}
}
The View is below where I am trying to get the sitename
<!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>
My Answer:
In your controller, you are trying to access the configuration values using `$this->data['configuration'][1]->value`, but in your config file, you are setting the configuration values to `$this->configuration`. So, you should access the values using `$this->configuration` instead.
Here is the updated code for your controller:
php
namespace App\Controllers;
use App\Controllers\BaseController;
use Config\MyConfig;
class Home extends BaseController {
public function index() {
$myconfig = new MyConfig;
$data = array('value' => $myconfig->configuration[1]->value);
$this->data['page_title'] = 'Dashboard';
return view('common/home', $data);
}
}
And in your view file, you can access the configuration value like this:
php
Make sure to adjust the index value `[1]` according to the specific configuration value you want to access.
Rate this post
5 of 5 based on 3842 votesComments