Cakephp3 Events and Listeners

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Cakephp3 Events and Listeners

Rob Cowie
Hi all. I have read the cakephp book on events in version 3, and I cannot seem to get it to work. I am missing a piece of comprehension. 

I have the event dispatch code in a controller:

$event = new Event('Controller.User.login', $this);
$this->eventManager()->dispatch($event);

and I have a model with the following code:

public function implementedEvents() {
    return [
        'Controller.User.login' => 'populateUserPrefs',
    ];
}

public function populateUserPrefs(Event $event, $user){
    Log::write(
        'info',
        'Prefs User Login: '
    );
}

But I am not hitting the populateUserPrefs function. I guess I need to add some code somewhere to register the model as a listener, but I cannot figure out how.


--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Cakephp3 Events and Listeners

Dr. Tarique Sani
Event listeners are implementations of Cake\Event\EventListener interface

also then you have to attach your event - something like

// Attach the UserStatistic object to the Order's event manager
$statistics = new UserStatistic();
$this->Orders->eventManager()->attach($statistics);


T



On Thu, Aug 21, 2014 at 9:26 PM, Rob Cowie <[hidden email]> wrote:
Hi all. I have read the cakephp book on events in version 3, and I cannot seem to get it to work. I am missing a piece of comprehension. 

I have the event dispatch code in a controller:

$event = new Event('Controller.User.login', $this);
$this->eventManager()->dispatch($event);

and I have a model with the following code:

public function implementedEvents() {
    return [
        'Controller.User.login' => 'populateUserPrefs',
    ];
}

public function populateUserPrefs(Event $event, $user){
    Log::write(
        'info',
        'Prefs User Login: '
    );
}

But I am not hitting the populateUserPrefs function. I guess I need to add some code somewhere to register the model as a listener, but I cannot figure out how.


--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.



--
=============================================================
The Conference Schedule Creator : http://shdlr.com

PHP for E-Biz : http://sanisoft.com
=============================================================

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Cakephp3 Events and Listeners

Rob Cowie
I have read the chapter, but my populateUserPrefs function does not fire.

Is any one able to flesh out the example, or tell me that actually I cannot have a Table Model class implement EventListener?



On Friday, 22 August 2014 04:36:03 UTC+1, Dr. Tarique Sani wrote:
Event listeners are implementations of Cake\Event\EventListener interface

also then you have to attach your event - something like

// Attach the UserStatistic object to the Order's event manager
$statistics = new UserStatistic();
$this->Orders->eventManager()->attach($statistics);

See <a href="http://book.cakephp.org/3.0/en/core-libraries/events.html#registering-listeners" target="_blank" onmousedown="this.href='http://www.google.com/url?q\75http%3A%2F%2Fbook.cakephp.org%2F3.0%2Fen%2Fcore-libraries%2Fevents.html%23registering-listeners\46sa\75D\46sntz\0751\46usg\75AFQjCNFUY_J4zecCGODYEZkP5p2x4Cz_0w';return true;" onclick="this.href='http://www.google.com/url?q\75http%3A%2F%2Fbook.cakephp.org%2F3.0%2Fen%2Fcore-libraries%2Fevents.html%23registering-listeners\46sa\75D\46sntz\0751\46usg\75AFQjCNFUY_J4zecCGODYEZkP5p2x4Cz_0w';return true;">http://book.cakephp.org/3.0/en/core-libraries/events.html#registering-listeners 

T



On Thu, Aug 21, 2014 at 9:26 PM, Rob Cowie <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="gvEPdvS9TcIJ" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">asbesto...@...> wrote:
Hi all. I have read the cakephp book on events in version 3, and I cannot seem to get it to work. I am missing a piece of comprehension. 

I have the event dispatch code in a controller:

$event = new Event('Controller.User.login', $this);
$this->eventManager()->dispatch($event);

and I have a model with the following code:

public function implementedEvents() {
    return [
        'Controller.User.login' => 'populateUserPrefs',
    ];
}

public function populateUserPrefs(Event $event, $user){
    Log::write(
        'info',
        'Prefs User Login: '
    );
}

But I am not hitting the populateUserPrefs function. I guess I need to add some code somewhere to register the model as a listener, but I cannot figure out how.


--
Like Us on FaceBook <a href="https://www.facebook.com/CakePHP" target="_blank" onmousedown="this.href='https://www.google.com/url?q\75https%3A%2F%2Fwww.facebook.com%2FCakePHP\46sa\75D\46sntz\0751\46usg\75AFQjCNG_OVGPrsW2S4xCvT0yb2leRWBsQg';return true;" onclick="this.href='https://www.google.com/url?q\75https%3A%2F%2Fwww.facebook.com%2FCakePHP\46sa\75D\46sntz\0751\46usg\75AFQjCNG_OVGPrsW2S4xCvT0yb2leRWBsQg';return true;">https://www.facebook.com/CakePHP
Find us on Twitter <a href="http://twitter.com/CakePHP" target="_blank" onmousedown="this.href='http://www.google.com/url?q\75http%3A%2F%2Ftwitter.com%2FCakePHP\46sa\75D\46sntz\0751\46usg\75AFQjCNGm99t3RztzLJAbpv22cUAOb0aFHw';return true;" onclick="this.href='http://www.google.com/url?q\75http%3A%2F%2Ftwitter.com%2FCakePHP\46sa\75D\46sntz\0751\46usg\75AFQjCNGm99t3RztzLJAbpv22cUAOb0aFHw';return true;">http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="gvEPdvS9TcIJ" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">cake-php+u...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="gvEPdvS9TcIJ" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">cake...@....
Visit this group at <a href="http://groups.google.com/group/cake-php" target="_blank" onmousedown="this.href='http://groups.google.com/group/cake-php';return true;" onclick="this.href='http://groups.google.com/group/cake-php';return true;">http://groups.google.com/group/cake-php.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" onmousedown="this.href='https://groups.google.com/d/optout';return true;" onclick="this.href='https://groups.google.com/d/optout';return true;">https://groups.google.com/d/optout.



--
=============================================================
The Conference Schedule Creator : <a href="http://shdlr.com" target="_blank" onmousedown="this.href='http://www.google.com/url?q\75http%3A%2F%2Fshdlr.com\46sa\75D\46sntz\0751\46usg\75AFQjCNGjmOhElKZG3xldNZlfvy5njPpUfA';return true;" onclick="this.href='http://www.google.com/url?q\75http%3A%2F%2Fshdlr.com\46sa\75D\46sntz\0751\46usg\75AFQjCNGjmOhElKZG3xldNZlfvy5njPpUfA';return true;">http://shdlr.com

PHP for E-Biz : <a href="http://sanisoft.com" target="_blank" onmousedown="this.href='http://www.google.com/url?q\75http%3A%2F%2Fsanisoft.com\46sa\75D\46sntz\0751\46usg\75AFQjCNEHab8B-s8KmlcSN62HbtMDVeCRyg';return true;" onclick="this.href='http://www.google.com/url?q\75http%3A%2F%2Fsanisoft.com\46sa\75D\46sntz\0751\46usg\75AFQjCNEHab8B-s8KmlcSN62HbtMDVeCRyg';return true;">http://sanisoft.com
=============================================================

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Cakephp3 Events and Listeners

Rob Cowie
OK, so i have my have my event listeners working now.

I was attempting to fire the event from a controller in a different plugin, and had not understood that you need to use the global event manager, and need to register the listeners in the bootstrap of the plugin that contains the listener.



On Friday, 22 August 2014 09:55:17 UTC+1, Rob Cowie wrote:
I have read the chapter, but my populateUserPrefs function does not fire.

Is any one able to flesh out the example, or tell me that actually I cannot have a Table Model class implement EventListener?



On Friday, 22 August 2014 04:36:03 UTC+1, Dr. Tarique Sani wrote:
Event listeners are implementations of Cake\Event\EventListener interface

also then you have to attach your event - something like

// Attach the UserStatistic object to the Order's event manager
$statistics = new UserStatistic();
$this->Orders->eventManager()->attach($statistics);

See <a href="http://book.cakephp.org/3.0/en/core-libraries/events.html#registering-listeners" target="_blank" onmousedown="this.href='http://www.google.com/url?q\75http%3A%2F%2Fbook.cakephp.org%2F3.0%2Fen%2Fcore-libraries%2Fevents.html%23registering-listeners\46sa\75D\46sntz\0751\46usg\75AFQjCNFUY_J4zecCGODYEZkP5p2x4Cz_0w';return true;" onclick="this.href='http://www.google.com/url?q\75http%3A%2F%2Fbook.cakephp.org%2F3.0%2Fen%2Fcore-libraries%2Fevents.html%23registering-listeners\46sa\75D\46sntz\0751\46usg\75AFQjCNFUY_J4zecCGODYEZkP5p2x4Cz_0w';return true;">http://book.cakephp.org/3.0/en/core-libraries/events.html#registering-listeners 

T



On Thu, Aug 21, 2014 at 9:26 PM, Rob Cowie <[hidden email]> wrote:
Hi all. I have read the cakephp book on events in version 3, and I cannot seem to get it to work. I am missing a piece of comprehension. 

I have the event dispatch code in a controller:

$event = new Event('Controller.User.login', $this);
$this->eventManager()->dispatch($event);

and I have a model with the following code:

public function implementedEvents() {
    return [
        'Controller.User.login' => 'populateUserPrefs',
    ];
}

public function populateUserPrefs(Event $event, $user){
    Log::write(
        'info',
        'Prefs User Login: '
    );
}

But I am not hitting the populateUserPrefs function. I guess I need to add some code somewhere to register the model as a listener, but I cannot figure out how.


--
Like Us on FaceBook <a href="https://www.facebook.com/CakePHP" target="_blank" onmousedown="this.href='https://www.google.com/url?q\75https%3A%2F%2Fwww.facebook.com%2FCakePHP\46sa\75D\46sntz\0751\46usg\75AFQjCNG_OVGPrsW2S4xCvT0yb2leRWBsQg';return true;" onclick="this.href='https://www.google.com/url?q\75https%3A%2F%2Fwww.facebook.com%2FCakePHP\46sa\75D\46sntz\0751\46usg\75AFQjCNG_OVGPrsW2S4xCvT0yb2leRWBsQg';return true;">https://www.facebook.com/CakePHP
Find us on Twitter <a href="http://twitter.com/CakePHP" target="_blank" onmousedown="this.href='http://www.google.com/url?q\75http%3A%2F%2Ftwitter.com%2FCakePHP\46sa\75D\46sntz\0751\46usg\75AFQjCNGm99t3RztzLJAbpv22cUAOb0aFHw';return true;" onclick="this.href='http://www.google.com/url?q\75http%3A%2F%2Ftwitter.com%2FCakePHP\46sa\75D\46sntz\0751\46usg\75AFQjCNGm99t3RztzLJAbpv22cUAOb0aFHw';return true;">http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at <a href="http://groups.google.com/group/cake-php" target="_blank" onmousedown="this.href='http://groups.google.com/group/cake-php';return true;" onclick="this.href='http://groups.google.com/group/cake-php';return true;">http://groups.google.com/group/cake-php.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" onmousedown="this.href='https://groups.google.com/d/optout';return true;" onclick="this.href='https://groups.google.com/d/optout';return true;">https://groups.google.com/d/optout.



--
=============================================================
The Conference Schedule Creator : <a href="http://shdlr.com" target="_blank" onmousedown="this.href='http://www.google.com/url?q\75http%3A%2F%2Fshdlr.com\46sa\75D\46sntz\0751\46usg\75AFQjCNGjmOhElKZG3xldNZlfvy5njPpUfA';return true;" onclick="this.href='http://www.google.com/url?q\75http%3A%2F%2Fshdlr.com\46sa\75D\46sntz\0751\46usg\75AFQjCNGjmOhElKZG3xldNZlfvy5njPpUfA';return true;">http://shdlr.com

PHP for E-Biz : <a href="http://sanisoft.com" target="_blank" onmousedown="this.href='http://www.google.com/url?q\75http%3A%2F%2Fsanisoft.com\46sa\75D\46sntz\0751\46usg\75AFQjCNEHab8B-s8KmlcSN62HbtMDVeCRyg';return true;" onclick="this.href='http://www.google.com/url?q\75http%3A%2F%2Fsanisoft.com\46sa\75D\46sntz\0751\46usg\75AFQjCNEHab8B-s8KmlcSN62HbtMDVeCRyg';return true;">http://sanisoft.com
=============================================================

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Cakephp3 Events and Listeners

Phan Tuan Kiet
In reply to this post by Rob Cowie
Hi all,
help me about cakephp 3.x in cell

<?php

namespace Templates\Event;

use Cake\Event\Event;
use Cake\Event\EventListenerInterface;
use Cake\Log\Log;

class UsersListener implements EventListenerInterface {

    public function implementedEvents() {
        return [
            'Cell.Templates.action_right_table' => 'action_right_table',
        ];
    }

    public function action_right_table(Event $event, array $action_right_selects) {
        $event->data['action_right_selects'] = ['test'=>'test'];
        return true;
    }

}

===================================

    public function action_right_table($action_right_selects = []) {
        $event = new Event('Cell.Templates.action_right_table', $this, ['action_right_selects' => $action_right_selects]);
        $this->eventManager()->dispatch($event);
        pr($event);die(); ==> null
    }


please help me. result $event null

thanks

Vào 22:56:08 UTC+7 Thứ Năm, ngày 21 tháng 8 năm 2014, Rob Cowie đã viết:
Hi all. I have read the cakephp book on events in version 3, and I cannot seem to get it to work. I am missing a piece of comprehension. 

I have the event dispatch code in a controller:

$event = new Event('Controller.User.login', $this);
$this->eventManager()->dispatch($event);

and I have a model with the following code:

public function implementedEvents() {
    return [
        'Controller.User.login' => 'populateUserPrefs',
    ];
}

public function populateUserPrefs(Event $event, $user){
    Log::write(
        'info',
        'Prefs User Login: '
    );
}

But I am not hitting the populateUserPrefs function. I guess I need to add some code somewhere to register the model as a listener, but I cannot figure out how.


--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.