Welcome Guest, Not a member yet? Register   Sign In
Hook system for CI app
#1

[eluser]bl00dshooter[/eluser]
Hello folks,

So, I have decided to start a small CMS with CI, for learning purposes.
It's going pretty smooth, but I decided to implement a hook system, so you can extend the CMS without touching the core files.

I have the following idea: To create a hook, a person creates a php file in my custom hooks folder, and simply redeclare every method of my controllers he/she wants.

Example:

I have a class called Users that has a method called showUser, like the following:

Code:
class Users {
function showUser($id) {
    echo "Welcome ". $id;
}
}

then, in the hooks folder, a file called Users_leavePlease.php:

Code:
function showUser($id) {
    echo "Please leave " . $id;
}

The problem is...php won't allow it, AFAIK.

Any ideas how can I make this work?

thanks,

bl00dshooter

PS: the examples aren't CI specific, I know.
#2

[eluser]WanWizard[/eluser]
If you want that, your hooks file needs to define a class that extends Users, and you will have to call that instead of your Users class. A bit similar to how CI itself deals with core class extensions (MY_...).

It is better to use an events system. Load the classes in your 'hooks' folder, and have their constructor register events, and link them to a method in the class. Your main code can then call an Event execute method, which calls all registered methods for that event.
Dan Horrigan wrote an excellent event library, so you don't have to invent one yourself, see here.
#3

[eluser]bl00dshooter[/eluser]
Hello,

thanks for your response.
This is my first time using this library and attempting a hook system, so I guess I'm doing something wrong.

My hook file code:

Code:
<?php
class Welcome_change {
    function __construct() {
        Events::register('cst_event', array('Welcome', 'index'));
        echo "yes...";
    }
}
?>

My controller code:

Code:
<?php
class Welcome extends Controller {
    function index() {
        $this->load->library('events');
        $this->load->view('welcome_message');
        require_once('hooks/welcome_change.php');
        $c = new welcome_change;
        $data['name'] = 'eduardo';
        Events::trigger('cst_event', $data, 'string');
    }
}

The output:

Quote:A PHP Error was encountered

Severity: Warning

Message: get_class() expects parameter 1 to be object, string given

Filename: libraries/Events.php

Line Number: 50


yes...

Any ideas?

Thanks,

bl00dshooter.
#4

[eluser]WanWizard[/eluser]
It tries to register the static call "Welcome::index". From the looks of it, it only supports static classes, so make sure your method can be called statically.

Where is this index defined? Is there a class called 'Welcome'? And is it loaded (as CI doesn't support autoloading)?
#5

[eluser]bl00dshooter[/eluser]
[quote author="WanWizard" date="1290392692"]It tries to register the static call "Welcome::index". From the looks of it, it only supports static classes, so make sure your method can be called statically.

Where is this index defined? Is there a class called 'Welcome'? And is it loaded (as CI doesn't support autoloading)?[/quote]

Welcome is the controller itself. Must it be something else?
#6

[eluser]WanWizard[/eluser]
Not very logical, triggering an event in the same controller, defined by an external class? Why not just call $this->index().
Events are handy to trigger calls in other classes, for example your hook class.

get_class requires an object as parameter, appearantly 'welcome::index' is not an object. I don't know what Dan intended with this code. I've wrote the event class for his Fuel framework which supports static and dynamic methods. Maybe you can reuse that...
#7

[eluser]bl00dshooter[/eluser]
Oh, I inverted the order of trigger and register, sorry. Trigger was suposed to go on the hook.

Gonna try yours soon, ty.
#8

[eluser]WanWizard[/eluser]
Note that it's not written for CI, but I expect conversion to a CI library shouldn't be to difficult.




Theme © iAndrew 2016 - Forum software by © MyBB