Welcome Guest, Not a member yet? Register   Sign In
Dataset from database accessible on all controllers and views
#1
Sad 

Hello thanks to the whole community and to whom you have dedicated your time to read this post.

The problem I have is that I have a table in the database called ´settings´ that inside has only two columns ´config_name´ and ´config_value´ with which I intend to control some functions of the application. The issue is that I capture all the data well but when I want to pass it globally so that all the drivers are able to read one or more given configurations, this does not happen, how can I do it? Greetings and thank you very much
Reply
#2

(This post was last modified: 06-16-2020, 11:59 PM by vitnibel.)

I think that almost every developer had a similar problem. here is my solution for CI4.

App/Config/SiteSettings.php
PHP Code:
namespace Config;

use 
CodeIgniter\Config\BaseConfig;

class 
SiteSettings extends BaseConfig
{
    // some persistent params
    public $dtf = [
        'short'     => 'd.m.y H:i'
    ];
    // params from DB
    public static $registrars = [
        '\App\Models\SiteSettings'
    ];


App/Models/SiteSettings.php
PHP Code:
namespace App\Models;

class 
SiteSettings 
{
    public static function siteSettings() 
    {
        $config = [];
        
        $db 
= \Config\Database::connect();
        $res $db->query("SELECT config_name, config_value FROM settings");
        
        
foreach($res->getResult() as $row)
        {
            $config[$row->config_name] = $row->config_value;
        }

        return $config;
    }


Common.php
PHP Code:
if(!function_exists('configItem'))
{
    function configItem($name$default NULL)
    {
        $cs config('SiteSettings');

        return $cs->{$name} ?? $default;
    }


and further anywhere in the code

in controller: $param_test = configItem('param_test', 'some_default_value');
or in view: <?php configItem('param_test'); ?>
or in BaseController: $this->settings = config('SiteSettings');

at the same time, access to the database table occurs only once upon the first call of the config('SiteSettings')
Reply
#3

Thank you very much for your input, something like this i have programmed, but i am using Codeigniter3 and I really can't find a way to make it work on CI3, Thanks
Reply
#4

For CI3 the same thing can be done through the hook system

hooks/Site_settings_hook.php
PHP Code:
class Site_settings_hook {
    function load() {
        $site_config = [];
        $ci = & get_instance();
        
        $qid 
$ci->db->query("SELECT config_name, config_value FROM settings");
        
        
if($qid && $qid->num_rows() > 0) {
            foreach($qid->result() as $row) {
                $site_config[$row->unit_name] = $row->unit_value;
            }

            $config = & get_config();
            
            $config 
array_merge($config$site_config);
        }
    }


config/hooks.php
PHP Code:
$hook['post_controller_constructor'][] = array(
    'class'    => 'Site_settings_hook',
    'function' => 'load',
    'filename' => 'Site_settings_hook.php',
    'filepath' => 'hooks'
); 

as a result, all values from the database become available in the application config
Reply
#5
Thumbs Up 
(This post was last modified: 06-19-2020, 04:52 AM by jreklund.)

Thank you very much for the help, I will implement it and then I will tell you the result, again thank you very much and greetings from Cuba
Reply
#6

Hello again, I have modified something in the code that you have put, because the CI super object did not work for me, in the end it was something like this:
PHP Code:
class Site_settings_hook extends CI_Model {

    function 
load() {

         
$this->obtain_config();

    }
    function 
obtain_config() {
        
$settings = [];

        
$query $this->db->query('SELECT config_name, config_value FROM gallery_config');

        foreach (
$query->result() as $row){
            
$settings[$row->config_name] = $row->config_value;
        }
        
$this->db->close();
        
$config = & get_config();

        
$config array_merge($config$settings);
    } 

but I still have the same problem, I have to declare each variable in each controller for each view I want to use, I would like to know if it is possible to do something like what I have developed outside of codeigniter for another project, which is something like this:
PHP Code:
    function obtain_config(){
            
$db = new db();
            
$config $_config = array();

            
$sql "SELECT config_name, config_value FROM settings";
            
$result $db->query($sql);

                while(
$row $result->fetch_array()){
                    
$config[$row['config_name']] = $row['config_value'];
                }
            
$db->close();

            return 
$config;

PHP Code:
$config obtain_config() 

so with this code when I want to get the value of a row in the confi_name table I just have to write.
PHP Code:
$config ['row name'
Thanks   Confused
Reply
#7

Hi

I have a little problem with this feature for CI4:

I have a "home controller" where I would like to use a function to test whether the value for "siteOffline" is 1 or 0.

If I query this directly in the controller, it works fine.

But if I create a public function of it, it is simply ignored.

Here is the code where my query works:
HomeController
PHP Code:
class Home extends BaseController
{
   public function index($locale '')
   {
        if(configItem('siteOffline')==1) {
            return view('Home/offline');
        }
... 


Here are the two snippets of code where it doesn't work:
HomeController

PHP Code:
class Home extends BaseController
{
   public function index($locale '')
   {
       $this->is_offline();

...

public function 
is_offline()
{
    if(configItem('siteOffline')==1) {
        return view('Home/offline');
    }


Where is my mistake?!?

Greetings, Kighlander
Reply
#8

(This post was last modified: 03-07-2021, 09:08 PM by vitnibel.)

Hi

As a result of the is_offline function call, just the string will be returned.

PHP Code:
class Home extends BaseController
{
   public function index($locale '')
   {
       $this->is_offline();
   }


Therefore, nothing further happens.

If you change the function to an example like this

PHP Code:
public function is_offline()
{
    if(configItem('siteOffline')==1) {
        echo view('Home/offline');
        exit;
    }


then after its execution either nothing will happen or the offline page will be shown.
Reply
#9

Oh, Okay...
Thank you very much, now it works fine !!!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB