Welcome Guest, Not a member yet? Register   Sign In
Dan Horrigan (Eric Barnes) Events Library - Help?
#1

[eluser]Devon Lambert[/eluser]
Hello all,

Trying to make use of this events library made by Dan Horrigan, and Eric Barnes.
https://github.com/dhorrigan/codeigniter-events

Unfortunately, I cannot figure out how to properly incorporate it in my code to listen/trigger events.

Can someone point me in the direction of a quick example of usage?

Thanks.
#2

[eluser]Eric Barnes[/eluser]
Hi Devon,

Here is an example to try and help.

1. Auto load the events library

2. Register a new hook inside your code. EX:
Code:
$data = array();
$event_return = Events::trigger('edit_user', $data, 'string');
var_dump($event_return);

3. Create an events class file. Which you will need to manually load first. I normally have these loaded based on if a module is active but you can do it any way you wish. An example file would be something like:
Code:
<?php defined('BASEPATH') or exit('No direct script access allowed');

class Events_Contact {

    public function __construct()
    {
        Events::register('edit_user', array($this, 'method_name'));
    }

    public function method_name()
    {
        echo 'it works';
    }
}

Basically the construct registers the method_name to the edit_user hook.

Here is an example where I integrated this into pyrocms which may help more:
https://github.com/pyrocms/pyrocms/commi...d38eb8c74b
#3

[eluser]Devon Lambert[/eluser]
Thanks for the fast response Eric.

I'll play around with this some more in the AM. :-)
#4

[eluser]Devon Lambert[/eluser]
Thanks again for your help on this Eric. It works like a charm.

I have 1 final question/stumping block that I'm facing...

How would you handle the situation of multiple events which in turn require multiple functions to fire given certain scenarios. To put this another way, I am trying to use this library in a user experience module.

1) Users are awarded experience points for completing certain actions on my site.
2) The module would listen for the required action and once complete would award the user the appropriate amount of points.

Obviously, your events library fits this scenario perfectly. However, let's say I have 20 actions that a user can complete for a given event, then wouldn't my "Experience_Events" class look something like below:

Code:
class Events_Experience {
    
    public function __construct()
    {
        
        Events::register('admin_controller', array($this, 'method_name1'));
        Events::register('admin_controller', array($this, 'method_name2'));
        Events::register('admin_controller', array($this, 'method_name3'));
        Events::register('admin_controller', array($this, 'method_name4'));
        Events::register('admin_controller', array($this, 'method_name5'));
        Events::register('admin_controller', array($this, 'method_name6'));
        Events::register('admin_controller', array($this, 'method_name7'));
        Events::register('admin_controller', array($this, 'method_name8'));
        Events::register('admin_controller', array($this, 'method_name9'));
        Events::register('admin_controller', array($this, 'method_name10'));
        Events::register('admin_controller', array($this, 'method_name11'));
        Events::register('admin_controller', array($this, 'method_name12'));
        Events::register('admin_controller', array($this, 'method_name13'));
        Events::register('admin_controller', array($this, 'method_name14'));
        Events::register('admin_controller', array($this, 'method_name15'));
        Events::register('admin_controller', array($this, 'method_name16'));
        Events::register('admin_controller', array($this, 'method_name17'));
        Events::register('admin_controller', array($this, 'method_name18'));
        Events::register('admin_controller', array($this, 'method_name19'));
        Events::register('admin_controller', array($this, 'method_name20'));

    }

    public function method_name1()
    {
        echo 'Experience Module is listening for the Admin Controller Event! it works!';
    }


    public function method_name2()
    {
        echo 'Experience Module is listening for the Admin Controller Event! it works!';
    }
    
    public function method_name3()
    {
        echo 'Experience Module is listening for the Admin Controller Event! it works!';
    }
    public function method_name4()
    {
        echo 'Experience Module is listening for the Admin Controller Event! it works!';
    }
    public function method_name5()
    {
        echo 'Experience Module is listening for the Admin Controller Event! it works!';
    }
    public function method_name6()
    {
        echo 'Experience Module is listening for the Admin Controller Event! it works!';
    }
    public function method_name7()
    {
        echo 'Experience Module is listening for the Admin Controller Event! it works!';
    }
    public function method_name8()
    {
        echo 'Experience Module is listening for the Admin Controller Event! it works!';
    }
    public function method_name9()
    {
        echo 'Experience Module is listening for the Admin Controller Event! it works!';
    }
    public function method_name10()
    {
        echo 'Experience Module is listening for the Admin Controller Event! it works!';
    }

    public function method_name11()
    {
        echo 'Experience Module is listening for the Admin Controller Event! it works!';
    }
    public function method_name12()
    {
        echo 'Experience Module is listening for the Admin Controller Event! it works!';
    }
    
    public function method_name13()
    {
        echo 'Experience Module is listening for the Admin Controller Event! it works!';
    }
    public function method_name14()
    {
        echo 'Experience Module is listening for the Admin Controller Event! it works!';
    }
    public function method_name15()
    {
        echo 'Experience Module is listening for the Admin Controller Event! it works!';
    }
    public function method_name16()
    {
        echo 'Experience Module is listening for the Admin Controller Event! it works!';
    }
    public function method_name17()
    {
        echo 'Experience Module is listening for the Admin Controller Event! it works!';
    }
    public function method_name18()
    {
        echo 'Experience Module is listening for the Admin Controller Event! it works!';
    }
    public function method_name19()
    {
        echo 'Experience Module is listening for the Admin Controller Event! it works!';
    }
    public function method_name20()
    {
        echo 'Experience Module is listening for the Admin Controller Event! it works!';
    }
}

Seems like a bit of overkill for one class, no? :-)
#5

[eluser]Eric Barnes[/eluser]
Yes you could it that way or just have it call one method which in turns does everything it needs:
Code:
class Events_Experience {
    
    public function __construct()
    {
        
        Events::register('admin_controller', array($this, 'method_name1'));
    }

    public function method_name1()
    {
        echo 'Experience Module is listening for the Admin Controller Event! it works!';
        $this->method_name2();
    }


    public function method_name2()
    {
        echo 'Experience Module is listening for the Admin Controller Event! it works!';
    }
#6

[eluser]Unknown[/eluser]
this event library really great but i still not understand where should i create the event class file

should i add in library folder but it has same filename with real library
or i have to do modification in real events.php file?
#7

[eluser]Eric Barnes[/eluser]
That is really up to you to decide. I always use the thirdparty folder to house anything related to that. Then scan it and initialize the classes that will be calling the hooks.

This has been added to the pyrocms dev branch on github if you want to see how it was done in it.




Theme © iAndrew 2016 - Forum software by © MyBB