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; \app\update_value.php PHP Code: use CodeIgniter\Events\Events; 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) { 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 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 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. |