0

Laravel 4

I'm trying to listen to events from a class in a namespace but can't work out how to set up the listener.

I have something like:

namespace My\Namespace
class Person  extends \Illuminate\Database\Eloquent\Model
...

In a separate class I have tried to listen for updates with

Event::listen('My\Namespace\Person.updated', function($person)
{
    Debugbar::info(Event::firing());
});

But that never gets called. I have tried lots of different variations of upper / lower case and putting a leading \ on the namespace but nothing works

If I change the listener to

Event::listen('*.updated', function($person)
{
    Debugbar::info(Event::firing());
});

The code runs and the debug output is : eloquent.updated: My\Namespace\Person

I just need to know what name to use in place of the * to make the handler specific to the namespaced class.

Thanks.

2
  • Just checked the docs. again and have worked out that this can be done with model observers, so use Person->observe(new PersonObserver) and pass the observer into the model. That solves the problem, but would still like to know if the above is possible? Commented Apr 24, 2014 at 15:05
  • i think you listen to certain events like 'component.action' that gets called within your code with Event::fire('component.action', [...]). haven't used it yet tho. Commented Apr 24, 2014 at 15:27

1 Answer 1

1

If you use Eloquent for saving your models to the database you might want to try:

Event::updated('yourcontroller@yourmethod');

I use this method and it's simple, clean and works great!

http://laravel.com/docs/eloquent#model-events

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

Comments

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.