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

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

@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); 
Reply
#4

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

It's giving another error  Sad

Code:
syntax error, unexpected '&', expecting ')'
Reply
#6

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

It was giving error too Sad
Reply
#8

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#9

function() use (&$array) {....}
Reply
#10

@iRedds, Thanks.

I saw that before but could not remember where it was.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB