简体   繁体   English

Cakephp事件已注册但未触发

[英]Cakephp Events registered but not firing

hi Guy's like many people I'm having dificulties Implementing Events into my Cakephp Application It would be really cool if somebody could point out where I messed im Using CakePHP v2.7 I allso followed Martin Bean's Tutorial on the Subject: http://martinbean.co.uk/blog/2013/11/22/getting-to-grips-with-cakephps-events-system/ 嗨Guy和很多人一样,我很难将事件实现到我的Cakephp应用程序如果有人能指出我搞砸了我在哪里使用CakePHP v2.7真的很酷我也跟着Martin Bean的主题教程: http:// martinbean .co.uk /博客/十一分之二千零十三/第22 /获取到握把与- cakephps事件系统/


My code is as Follows: App/Event/UserListener.php 我的代码如下:App / Event / UserListener.php

<?php
App::uses('CakeEventListener', 'Event');

class UserListener implements CakeEventListener {

public function implementedEvents() {
    #@:off;
    return array(
        'Model.User.test' => 'test',
        'Model.User.created' => 'sendActivationEmail',
    );
    #@:on;
}

public function test($event) {
    for ($i = 0; $i < 10; $i++) {
        echo "string<br />";
    }
    CakeLog::write('CakeEvents', 'Testevent Fired');
}

public function sendActivationEmail($event) {

}

}

in App/Model/User.php 在App / Model / User.php中

<?php
App::uses('AppModel', 'Model');
App::uses('Role', 'Model');
App::uses('Clroute', 'Model');
App::uses('CakeEvent', 'Event');

public function afterFind($results, $primary = true) {
    $Event = new CakeEvent('Model.User.test', $this, array(
    #@:off;
            'id' => 66,
            'data' => 'ppx'
        )
    );
    #@:on;
    CakeLog::write('CakeEvents', 'Testevent dispatch in UserModel');
    $this -> getEventManager() -> dispatch($Event);
    if ($results[0]['Salon']['id'] == null) {
        $results[0]['Salon'] = FALSE;
    }
    return $results;
}

And last but not least in App/Config/bootstrap.php 最后但并非最不重要的是在App / Config / bootstrap.php中

App::uses('ClassRegistry', 'Utility');
App::uses('UserListener', 'Event');
$user = ClassRegistry::init('User');
$user->getEventManager()->attach(new UserListener());

It would be great if you could Help me out because I'm really Stuck there and Cakephp's Documentation on the subject is kind of hazy allso :-/ 如果你可以帮助我,那将是很棒的,因为我真的被困在那里,Cakephp关于这个主题的文档也有点模糊: - /

Big THX in advance!! 大THX提前!! regards Michael 问迈克尔

I would advise against directly attaching the event listeners in the model (as suggested in your own answer). 我建议不要在模型中直接附加事件监听器(如您自己的答案所示)。 This somewhat defeats the point of using events in the first place. 这在一定程度上打败了使用事件的重点。 One of the strengths of using events is that it allows you to develop more extendable code that can be easily overridden; 使用事件的一个优点是它允许您开发更容易被覆盖的可扩展代码; attaching the listener directly from the model goes against this concept. 直接从模型附加监听器违背了这个概念。

If you're listener doesn't appear to be called despite attaching it to your model in bootstrap.php you may need to globally attach it:- 如果您的监听器似乎没有被调用,尽管它在bootstrap.php附加到您的模型,您可能需要全局附加它: -

// In app/Config/bootstrap.php
App::uses('CakeEventManager', 'Event');
App::uses('UserListener', 'Lib/Event');
CakeEventManager::instance()->attach(new UserListener());

This is instead of attaching it directly to the User model. 这不是将其直接附加到User模型。

scrap the code in App/Config/bootstrap.php instead add this line to your Model: 废弃App / Config / bootstrap.php中的代码,而不是将此行添加到您的模型中:

$user->getEventManager()->attach(new UserListener());

and enshure to add this to your Model aswell: 并确保将此添加到您的模型中:

App::uses('UserListener', 'Event');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM