Welcome Guest, Not a member yet? Register   Sign In
Alternative $this->config>set_item($item, $value)
#1

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!
Reply
#2

(This post was last modified: 12-01-2019, 06:42 PM by dave friend.)

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!

Reply
#3

(This post was last modified: 12-01-2019, 09:21 PM by simonickalexs.)

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!

Reply
#4

(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'); 
Reply
#5

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




Theme © iAndrew 2016 - Forum software by © MyBB