CodeIgniter Forums
How to update value by Events in CodeIgniter 4 - 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: How to update value by Events in CodeIgniter 4 (/showthread.php?tid=78988)

Pages: 1 2


How to update value by Events in CodeIgniter 4 - ciddict - 04-05-2021

Here is my code:

\app\Controllers\My_controller.php

PHP Code:
namespace App\Controllers;

use 
CodeIgniter\Events\Events;

class 
My_controller extends App_Controller {

    function __construct() {
        parent::__construct();
        require_once(APPPATH "update_value.php");
    }

    function index() {
        $array = array("foo""bar");
        Events::trigger('add_value_to_array'$array);

        print_r($array);
    }



\app\update_value.php

PHP Code:
use CodeIgniter\Events\Events;

Events::on('add_value_to_array', function($array) {
    error_log("add_value_to_array" PHP_EOL3"events.txt");
    array_push($array"new");
    return $array;
}); 

I'm getting this output:


PHP Code:
Array ( [0] => foo [1] => bar 

But it's triggering the event because it's giving a log containing this text:


Code:
add_value_to_array

I want the updated array containing the 'new' item. My expected result:

PHP Code:
Array ( [0] => foo [1] => bar [2] => new  

Is it possible? Thanks in advance.


RE: How to update value by Events in CodeIgniter 4 - InsiteFX - 04-05-2021

This is not tested but I think you need to pass the array by reference.

PHP Code:
Events::on('add_value_to_array', function(&$array) {
    error_log("add_value_to_array" PHP_EOL3"events.txt");
    $array[] = 'new';
}); 

You can try your way with array_pust but then you making an extra function call.


RE: How to update value by Events in CodeIgniter 4 - ciddict - 04-05-2021

@InsiteFX, Thanks.

With your code, I'm getting this error:

Code:
Parameter 1 to App\Controllers\My_controller::{closure}() expected to be a reference, value given

This is heading to this line:

PHP Code:
Events::trigger('add_value_to_array'$array); 



RE: How to update value by Events in CodeIgniter 4 - InsiteFX - 04-05-2021

Try adding the reference to it.

PHP Code:
Events::trigger('add_value_to_array', &$array); 

Not sure if it will work this is new grounds to me.


RE: How to update value by Events in CodeIgniter 4 - ciddict - 04-05-2021

It's giving another error  Sad

Code:
syntax error, unexpected '&', expecting ')'



RE: How to update value by Events in CodeIgniter 4 - InsiteFX - 04-05-2021

Not sure if any of this will work looking at the events class none of the methods are returning anything.

PHP Code:
$return Events::trigger('add_value_to_array'$array); 

I do not think it will work but maybe, you may be able to do something using sessions.


RE: How to update value by Events in CodeIgniter 4 - ciddict - 04-08-2021

It was giving error too Sad


RE: How to update value by Events in CodeIgniter 4 - InsiteFX - 04-08-2021

If you look at the Events Library it doe's not return anything, so the only thing
you might be able to do is use sessions or a registry class.


RE: How to update value by Events in CodeIgniter 4 - iRedds - 04-08-2021

function() use (&$array) {....}


RE: How to update value by Events in CodeIgniter 4 - InsiteFX - 04-08-2021

@iRedds, Thanks.

I saw that before but could not remember where it was.