Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
where to register events?
  • The application I'm building has some "game-ified" elements to it. When users do certain things throughout the site, they can earn points and tokens. I've got a user_activity table and model to track the points and tokens and I'd like to use Fuel's Event class to trigger methods in the user_activity model. I'm not sure where to put all my Event::register() statements though. All the callback methods will be in the user_activity model, so I'd like to register them all there. That seems intuitive to me. And then be able to trigger events in all my other models. I have it working right now, but I have a sinking feeling that this really isn't the best solution... Let me know what you think. In my user_activity model, I wrote an init function that registers all the events :
    public static function init() {
      Event::register( 'earned_token', 'Model_User_Activity::earned_token' );
      Event::register( 'checkin', 'Model_User_Activity::checkin' );
    }
    
    Then in my app/bootstrap.php I added this line : Model_User_Activity::init(); (Autoloading the user_activity class did not work...) So yeah, this works, but I'm not sure it's the right way to go about it. Please let me know if there's a better way! Thanks very much for any help!
  • If your class is called Model_User_Activity, this is what you should autoload (and not user_activity).
    FuelPHP doesn't care that it's a model, a class is a class...
  • Sorry, I wasn't being specific. I did try to autoload Model_User_Activity. I was just casually referring to it as "user_activity" in my OP.
  • If you add it to the always_load classes list, and it's in app/classes/model/user in a file called activity.php, autoloading should work without problems. I use this in every application.
  • More confusion from me not being clear - my apologies! :) You're right - autoloading works fine, that's not the problem. What I meant when I said it doesn't work is that when the class autoloads, it does not successfully register my events. The goal is simply to get my events registered before any of the triggers are called. If autoloading a class is the best way to do that, then great - I could use a little help in getting that working because it's not working for me currently. If not, then the more general question is what is the best way to register my events? As I said in my OP, I added a line in my app/bootstrap.php that essentially registers the events. And this works fine, but it strikes me as probably not the best way to do it, right?
  • I think the mistake is that the static class init method is called _init(). Note the underscore.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion