Welcome Guest, Not a member yet? Register   Sign In
How to load personal config file in BaseController and have it available everywhere
#1

I created my personal configuration file "MyConfig" inside the Config folder like this:


PHP Code:
namespace Config;

use 
CodeIgniter\Config\BaseConfig;

class 
MyConfig extends BaseConfig
{
    public $ada_default_lang 'en';
    public $ada_site_name 'Site name';



Now I wish I had it available everywhere.

I tried to insert it into BaseController's initController function in several ways, but without success


PHP Code:
$myconfig config('\Config\\MyConfig');
or
$myconfig config('MyConfig');
or
$this->myconfig = new \Config\MyConfig;
or
$this->myconfig config('MyConfig'); 



In my controller I tried to retrieve the variables with $myconfig->ada_site_name or $this->myconfig-> ada_site_name, but it fails, how can I fix?
Reply
#2

(This post was last modified: 02-10-2022, 05:13 AM by kenjis.)

BaseController.php
PHP Code:
    public function initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
    {
        // Do Not Edit This Line
        parent::initController($request$response$logger);

        // Preload any models, libraries, etc, here.
        $this->myconfig config('MyConfig');
    

Home.php
PHP Code:
<?php

namespace App\Controllers;

class 
Home extends BaseController
{
    public function index()
    {
        return $this->myconfig->ada_site_name;
    }



I see "Site name".
Reply
#3

(02-10-2022, 05:12 AM)kenjis Wrote: BaseController.php
PHP Code:
    public function initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
    {
        // Do Not Edit This Line
        parent::initController($request$response$logger);

        // Preload any models, libraries, etc, here.
        $this->myconfig config('MyConfig');
    

Home.php
PHP Code:
<?php

namespace App\Controllers;

class 
Home extends BaseController
{
    public function index()
    {
        return $this->myconfig->ada_site_name;
    }



I see "Site name".

ok, so it also works with echo $ this->myconfig->ada_site_name, but not in the view

in my view

Code:
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <p><?= $this->myconfig->ada_site_name ?></p>
</body>

</html>

Undefined property: CodeIgniter\View\View::$myconfig
Reply
#4

(This post was last modified: 02-10-2022, 09:15 AM by ikesela.)

you need too pass config data to view

example

view('app/view/file',[ 'config' => $this->myconfig])


so can call in view: $config->myconfig->ada_site_name

controller and view are two different thing.
Reply
#5

(02-10-2022, 09:12 AM)ikesela Wrote: you need too pass config data to view

example

view('app/view/file',[ 'config' => $this->myconfig])


so can call in view: $config->myconfig->ada_site_name

controller and view are two different thing.

Yes, I know that, but in CI3 it was possible to call them anywhere with $this->config->item ('ada_site_name');

Isn't there a way anymore?
Reply
#6

(This post was last modified: 02-10-2022, 04:34 PM by kenjis.)

As you see `Undefined property: CodeIgniter\View\View::$myconfig`,
you have to set the variable `CodeIgniter\View\View::$myconfig`.

> Yes, I know that, but in CI3 it was possible to call them anywhere with $this->config->item ('ada_site_name');

It is CI3 way, CI4 is not.

BaseController.php
PHP Code:
    public function initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
    {
        // Do Not Edit This Line
        parent::initController($request$response$logger);

        // Preload any models, libraries, etc, here.
        $this->myconfig config('MyConfig');
        $renderer = \Config\Services::renderer();
        $renderer->setVar('myconfig'$this->myconfig);
    


View
PHP Code:
<body>
<
p><?= esc($myconfig->ada_site_name?></p>
</body> 
Reply
#7

(This post was last modified: 02-14-2022, 02:03 AM by serialkiller.)

(02-10-2022, 04:22 PM)kenjis Wrote: As you see `Undefined property: CodeIgniter\View\View::$myconfig`,
you have to set the variable `CodeIgniter\View\View::$myconfig`.

> Yes, I know that, but in CI3 it was possible to call them anywhere with $this->config->item ('ada_site_name');

It is CI3 way, CI4 is not.

BaseController.php
PHP Code:
    public function initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
    {
        // Do Not Edit This Line
        parent::initController($request$response$logger);

        // Preload any models, libraries, etc, here.
        $this->myconfig config('MyConfig');
        $renderer = \Config\Services::renderer();
        $renderer->setVar('myconfig'$this->myconfig);
    


View
PHP Code:
<body>
<
p><?= esc($myconfig->ada_site_name?></p>
</body> 

I tried like this:
Code:
config('MyConfig')->side_site_name;
is it valid as I use? This way I can get the values without doing anything else, it also applies to
PHP Code:
servive('request')->getPost() 
and all the other request methods, or
Code:
service('session')->variable;
This way I can get the values wherever I need them without having to do anything else
Reply
#8

PHP Code:
config('MyConfig')->side_site_name
is valid. I think you can't use config('My Config'). (a space after My)

config('MyConfig') returns the MyConfig object.
Reply
#9

(02-14-2022, 01:58 AM)kenjis Wrote:
PHP Code:
config('MyConfig')->side_site_name
is valid. I think you can't use config('My Config'). (a space after My)

config('MyConfig') returns the MyConfig object.

Yes, I wrote them wrong Wink

Are the other methods also used correctly instead of instantiating the class on the controller and then passing them to the view?
Put simply, is it a best practice?
Reply
#10

You can also do it like this using the config helper.

PHP Code:
<?php
    
// add to the top of your html view file.
    $myconfig config('MyConfig');
?>

<p>
    <!-- then call like this. -->
    <?= $myconfig->ada_site_name ?>
</p> 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB