Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use event trigger library ?

trying to understand how to use this Event listener library in codeigniter https://github.com/ericbarnes/CodeIgniter-Events

I am trying to send email on Post Submit. My post controller is as below

/**
* Posts
*/
class Posts extends MX_Controller
{

    function __construct(argument)
    {
        # code...
    }

    function post_new()
    {
        // all form validation and submission code

        Events::trigger('add_post', 'system_events', 'string');
    }
}

I have created one controller where I will register all system event

/**
* System Events
*/
class System_Events extends MX_Controller
{

    function __construct(argument)
    {
        Events::register('add_post', array($this, 'shoot_email'));
    }


    function shoot_email()
    {
        // all email code here
    }

}

But this is not sending any email on post submission. I don't actually understand how to use this event library.

I am also fine if there is any other way to register and trigger events. But not hard coded in to the system but kind of API.

like image 706
Code Lover Avatar asked Nov 23 '25 03:11

Code Lover


1 Answers

The System_Events is never called from your post controller, try this

class System_Events extends MX_Controller{

    function __construct($eventName,$methodName){
        $this->load->library('events');
        Events::register($eventName, array($this, $methodName));
    }


    function shoot_email(){
        // all email code here
        return 'Mail Send';
    }

}

Now extend the posts controller from your system_events

class Posts extends System_Events{

    function __construct(argument){
        parent::__construct('add_post','shoot_email');//call the parent class constructor    
    }

    function post_new()
    {
        // all form validation and submission code

        Events::trigger('add_post', 'system_events', 'string');
    }
}
like image 104
Nouphal.M Avatar answered Nov 24 '25 20:11

Nouphal.M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!