CodeIgniter Forums
Alternative $this->config>set_item($item, $value) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Alternative $this->config>set_item($item, $value) (/showthread.php?tid=74963)



Alternative $this->config>set_item($item, $value) - simonickalexs - 12-01-2019

I notice that this function is not in CI4. But, I need this function to dynamically inject (temporary) array string to public $item = [] from \Config\App.php. Is there any alternative or maybe there's a workaround I'm not noticing?

Thank you!


RE: Alternative $this->config>set_item($item, $value) - dave friend - 12-01-2019

All the config files are classes where all the properties are public. That means that once you create an instance of a config you can modify the properties however you see fit.

PHP Code:
<?php namespace App\Controllers;

use 
Config;

class 
Home extends BaseController
{
    public function index()
    {
       $viewConfig = new Config\View();
       $viewConfig->saveData false;
       // do stuff
      }


You can dynamically add properties to the class

PHP Code:
$config = new Config\App();
$config->adHoc = ["Hello ""World""!"];
foreach (
$viewConfig->adHoc as $value)
{
    echo 
$value;  // Hello World!




RE: Alternative $this->config>set_item($item, $value) - simonickalexs - 12-01-2019

Thanks for the reply. It seems that it didn't work to push array to my public $item = [...];. Here's my code:

app\config\Packages
PHP Code:
<?php namespace Config;

use 
CodeIgniter\Config\BaseConfig;

class 
Packages extends BaseConfig
{
  public $footer_js = ['nuxt.js''jquery.js''idk.js'];
  // ......


app\Controllers\Test
PHP Code:
<?php namespace App\Controllers;

class 
Test extends BaseController
{
    public function index()
    {
        $ci = new \Config\Packages();

        $ci->footer_js 'vue.js';

        foreach($ci->footer_js as $v)
        {
            echo $v;
            echo '<br/>';
        

The output should show nuxt.js, jquery.js, idk.js, and vue.js. But instead, it just show only vue.js. 

In real case, I really want to dynamically add Javascript files to footer page. I have this helper works on CI3, but I'm still confused how can I make it work on CI4-rc. Thank you!

-------

(12-01-2019, 06:38 PM)dave friend Wrote: All the config files are classes where all the properties are public. That means that once you create an instance of a config you can modify the properties however you see fit.

PHP Code:
<?php namespace App\Controllers;

use 
Config;

class 
Home extends BaseController
{
    public function index()
    {
       $viewConfig = new Config\View();
       $viewConfig->saveData false;
       // do stuff
      }


You can dynamically add properties to the class

PHP Code:
$config = new Config\App();
$config->adHoc = ["Hello ""World""!"];
foreach (
$viewConfig->adHoc as $value)
{
    echo $value;  // Hello World!




RE: Alternative $this->config>set_item($item, $value) - freddy.cruze - 02-02-2021

(12-01-2019, 09:16 PM)simonickalexs Wrote: Thanks for the reply. It seems that it didn't work to push array to my public $item = [...];. Here's my code:

app\config\Packages
PHP Code:
<?php namespace Config;

use 
CodeIgniter\Config\BaseConfig;

class 
Packages extends BaseConfig
{
  public $footer_js = ['nuxt.js''jquery.js''idk.js'];
  // ......


app\Controllers\Test
PHP Code:
<?php namespace App\Controllers;

class 
Test extends BaseController
{
    public function index()
    {
        $ci = new \Config\Packages();

        $ci->footer_js 'vue.js';

        foreach($ci->footer_js as $v)
        {
            echo $v;
            echo '<br/>';
        

The output should show nuxt.js, jquery.js, idk.js, and vue.js. But instead, it just show only vue.js. 

In real case, I really want to dynamically add Javascript files to footer page. I have this helper works on CI3, but I'm still confused how can I make it work on CI4-rc. Thank you!

At this point you have discovered the error, but I only make the comment for those of us who come to this post for one reason or another and we do not see a conclusion.

Obviously the error is that you are replacing the Scripts array with a single value, instead you can use array_push

PHP Code:
$ci = new \Config\Packages();
array_push($ci->footer_js'vue.js'); 



RE: Alternative $this->config>set_item($item, $value) - paulbalandan - 02-02-2021

$ci->footer_js[] = 'vue.js';