Welcome Guest, Not a member yet? Register   Sign In
How to update value by Events in CodeIgniter 4
#1

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.
Reply


Messages In This Thread
How to update value by Events in CodeIgniter 4 - by ciddict - 04-05-2021, 09:36 AM



Theme © iAndrew 2016 - Forum software by © MyBB