Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Can I pass controller data to an observer?
  • I have an "after_insert" observer that creates a database entry which logs the insert event. Is there any way to pass controller data to my observer? My observer definition looks like this:

    'Orm\Observer_Category' => array(
    'events' => array('before_insert', 'after_insert'),
    ),

    Ideally, I'd like to have the observer accept a second parameter, so instead of:

    public function after_insert(Model $ticket) {}

    I would instead like to have:

    public function after_insert(Model $ticket, $user_data) {}

    Where I can pass data from my base controller to the $user_data parameter. Is this possible?
  • HarroHarro
    Accepted Answer
    No, the observer observes actions in the ORM Model, it is not aware something like a controller even exists.

    The observer does have access to the current ORM object, so you could store the data in the ORM object in your controller, and retrieve it in the observer. You need to be on 1.5+ for this, earlier versions of the ORM don't support custom data.

    To store custom data, just do

    // assume this is your model object
    $ticket->my_special_user_data_property = $user_data;
  • I'm having trouble upgrading my project to 1.5, but that is a topic for a different post.

    I have an abstract base controller which every other controller in my application extends. It handles site-wide necessities such as authentication and storage of user data. I have multiple observers which need access to data that is stored in the base controller. Rather than explicitly passing the necessary data to each model, I created a static $instance variable in my base controller, and in my base controller's before() method, I have:

    if (!isset(self::$instance))
    {
    self::$instance = $this;
    }
    Now, from my observer method, I can:

    $controller = \Controller\Base::$instance;

    and subsequently I can access all of the data stored there from the context of the called controller. It seems to work just fine. Is there anything fundamentally wrong with this approach?
  • It creates a tight coupling with your controller, and that's considered bad practice. So I wouldn't use it, but if it works for you...
  • Bummer, but I see what you mean. If I were to use the model from, say, the command line interface, it would fail. Or, if I were to implement an API that didn't extend my base controller, that would also be an issue. I guess I'll fix it after I figure out how to (properly) upgrade to 1.5.
  • What trouble do you have? Maybe I can help?

    You might want to upgrade to 1.6/develop btw, it's more stable then 1.5 at the moment.
  • ARGH!!! It happened today! An API call that saves ticket data triggered the after_update observer, and it broke everything...damn. Stupid controller coupling.

Howdy, Stranger!

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

In this Discussion