3

I've read a lot about Event::queue but I just cant get my head around it, so i have something like:

Event::listen('send_notification');

and in the controller I use

Event::fire('send_notification');

But because this takes sometime before sending the user to somewhere else, I instead want to use

Event::queue('send_notification');

To fire the event after the user has been redirected, but I don't know how.

(In the app/config/app.php i have the queue driver set to sync)

EDIT:

a small note about firing the event ,u can do all ur work just like normal ,and add all the Event::flush() as a filter ,then just call that filter through ->after() or afterFilter().

2
  • Queued events have nothing to do with the queue driver. Event::queue is just to "prepare" a event and fire it late that request using flush() Commented Dec 29, 2014 at 15:43
  • @lukasgeiter thanks for that ,i was just lost between both ,but now lets say i have more than one event ,should i put them into the flush() as an array or else ? Commented Dec 29, 2014 at 16:12

1 Answer 1

4

First, let me make something clear. Event::queue has nothing to do with the Queue facade and the query driver in the config. It won't enable you to fire the event after the request has happened.

But you can delay the firing of an event and therefore "prepare" it.

The usage is pretty basic. Obviously you need one or many Event::listen (well it works without them but makes no sense at all)

Event::listen('send_notification', function($text){
    // send notification
});

Now we queue the event:

Event::queue('send_notification', array('Hello World'));

And finally, fire it by calling flush

Event::flush('send_notification');

In your comment you asked about flushing multiple events at once. Unfortunately that's not really possible. You have to call flush() multiple times

Event::flush('send_notification');
Event::flush('foo');
Event::flush('bar');

If you have a lot of events to flush you might need to think about your architecture and if it's possible to combine some of those into one event with multiple listeners.

Flushing the Event after redirect

Event::queue can't be used to fire an event after the request lifecycle has ended. You have to use "real" queues for that.

Sign up to request clarification or add additional context in comments.

6 Comments

thats exactly what i was looking for ,also where do u usually place the Event::flush() code ? ,i was thinking about using it after the return Redirect::back(); but u cant execute anything after the return so ?
thanx,it works now , but it takes the same time the usual Event::fire takes.
Too bad. Then you'll have to use "real" queues as I've written above. I'll also remove the code example since it doesn't help at all...
okey ,one last question ,how to check if an event was queued ? consider somthing like this ($this->getNotified() == true) ? Event::queue('update_subscribe') : Event::queue('remove_subscribe');
I don't think you can do that and I don't think you should need to. Note that you can call flush() regardless if the event has been queued or not (if not it will just do nothing)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.