CodeIgniter Forums
CI4 Event methods not firing - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: CI4 Event methods not firing (/showthread.php?tid=82457)



CI4 Event methods not firing - pistolpete16 - 07-12-2022

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!


RE: CI4 Event methods not firing - kenjis - 07-13-2022

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()?


RE: CI4 Event methods not firing - pistolpete16 - 07-13-2022

(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


RE: CI4 Event methods not firing - pistolpete16 - 07-13-2022

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