Welcome Guest, Not a member yet? Register   Sign In
CI4 Event methods not firing
#1

Hi, I am trying to use Events to call some methods but the methods I'm calling aren't being fired.  Here is an example:
Config\Autoload.php:
Code:
public $classmap = [
        'MyClass' => APPPATH . 'Libraries/MyClass.php',
    ];
Libraries\MyClass:
Code:
namespace App\Libraries;

class MyClass
{
    public function testMethod() {
        var_dump("Test success");
        exit;
    }
}
Config\Events.php:
Code:
use App\Libraries\MyClass;

$MyClass = new MyClass();
Events::on('pre_system', [$MyClass, 'testMethod']);
I am expecting 'Test success' to print before the page is loaded, but currently the page is loading normally without printing 'Test success' at all and I can't seem to figure out what I'm doing wrong.

Thanks for your help!
Reply
#2

It seems your code works fine, but you need to remove `exit`.
You should not use exit, because it terminates the framework working.

What if you write something to log file, not var_dump()?
Reply
#3

(This post was last modified: 07-13-2022, 03:44 AM by pistolpete16.)

(07-13-2022, 12:27 AM)kenjis Wrote: It seems your code works fine, but you need to remove `exit`.
You should not use exit, because it terminates the framework working.

What if you write something to log file, not var_dump()?

Thank you for your reply! Even if I take out the exit, this code still does not work for me.  'Test success' never gets printed on my page.

This is just a simplified example I am using, which I have tried in my current project without success.  I am upgrading my company's project from CI3 and my goal is to check a log file and a session file on the post_controller_constructor event and then write to a session file on the post_system event.  I am having trouble getting these methods to actually run on those events though, just like in my example
Reply
#4

I was using version 4.1.8, once I upgraded the system files to version 4.2.1 the event methods started to work!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB