• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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
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
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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.