-
serialkiller Member
  
-
Posts: 122
Threads: 45
Joined: Jan 2016
Reputation:
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?
-
kenjis Administrator
      
-
Posts: 3,671
Threads: 96
Joined: Oct 2014
Reputation:
230
02-10-2022, 05:12 AM
(This post was last modified: 02-10-2022, 05:13 AM by kenjis.)
BaseController.php
PHP Code: public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $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".
-
serialkiller Member
  
-
Posts: 122
Threads: 45
Joined: Jan 2016
Reputation:
1
(02-10-2022, 05:12 AM)kenjis Wrote: BaseController.php
PHP Code: public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $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
-
ikesela Member
  
-
Posts: 155
Threads: 0
Joined: Nov 2020
Reputation:
7
02-10-2022, 09:12 AM
(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.
-
kenjis Administrator
      
-
Posts: 3,671
Threads: 96
Joined: Oct 2014
Reputation:
230
02-10-2022, 04:22 PM
(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 $request, ResponseInterface $response, LoggerInterface $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>
-
serialkiller Member
  
-
Posts: 122
Threads: 45
Joined: Jan 2016
Reputation:
1
02-14-2022, 01:49 AM
(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 $request, ResponseInterface $response, LoggerInterface $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
-
kenjis Administrator
      
-
Posts: 3,671
Threads: 96
Joined: Oct 2014
Reputation:
230
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.
-
serialkiller Member
  
-
Posts: 122
Threads: 45
Joined: Jan 2016
Reputation:
1
(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
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?
-
InsiteFX Super Moderator
     
-
Posts: 6,747
Threads: 346
Joined: Oct 2014
Reputation:
247
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 )
|