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 >
<div >
<?= $this->include('layouts/header'); ?>
<?= $this->include('layouts/sidebar'); ?>
<!-- Content Wrapper. Contains page content -->
<div >
<!-- Content Header (Page header) -->
<section >
<div >
<div >
<div >
<h1><?php echo $page_title; ?></h1>
</div>
<div >
<ol >
<li ><a href="#">Home</a></li>
<li ><?php echo $page_title; ?></li>
</ol>
</div>
</div>
</div><!-- /.container-fluid -->
</section>
<!-- Main content -->
<section >
<!-- Default box -->
<div >
<div >
<h3 ><?php echo $page_title; ?></h3>
</div>
<div >
<?= $this->renderSection('content'); ?>
</div>
<!-- /.card-body -->
<div >
Footer
</div>
<!-- /.card-footer-->
</div>
<!-- /.card -->
</section>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
<?= $this->include('layouts/footer'); ?>
<!-- Control Sidebar -->
<aside >
<!-- 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>
CodePudding user response:
You're unable to pass the configuration values to the view because you declare the configuration object $myconfig = new MyConfig;
but it remains unused throughout the controller's index
method's body.
Reference:
Working With Configuration Files
All configuration object properties are public, so you access the settings like any other property:
$myconfig = new MyConfig;
// Access settings as object properties.
$settings = $myconfig->configuration;
CodePudding user response:
I think you might be after Registrars (https://codeigniter4.github.io/CodeIgniter4/general/configuration.html#registrars) to enable you to use custom configuration settings. The advantage of this is that you should then be able to call config('MyConfig')->setting_name
anywhere in your code using the default Codeigniter calls.
CodePudding user response:
By using the suggestion from both Anthony and Steven. I placed the following code in my base controller public function and I was able to refer the values fine:
$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;
then in my view I simple call setting that I want:
<?php echo $businessname; ?>