事件(Events)

Phalcon7 中几乎所有的组件都继承自 Phalcon\Di\Injectable,它们都可以通过 attachEvent 来设置对应事件的回调函数。

使用示例(Usage Example)

以下面示例中,我们使用 attachEvent 设置MySQL连接中产生的事件的回调函数:

  1. <?php
  2.  
  3. use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
  4.  
  5. $connection = new DbAdapter(
  6. array(
  7. "host" => "localhost",
  8. "username" => "root",
  9. "password" => "secret",
  10. "dbname" => "invo"
  11. )
  12. );
  13.  
  14. $connection->attachEvent('beforeExecutePrepare', function($event, $statement, $prevData = NULL){
  15.  
  16. });
  17.  
  18. $connection->query("SELECT * FROM products p WHERE p.status = 1");

事件的执行

几乎所有 Phalcon 对象都可以通过 fireEvent()fireEventCancel() 方法来执行事件:

  1. <?php
  2.  
  3. $connection->fireEvent("my-component:afterSomeTask", $extraData);

事件管理器(Events Manager)

此组件的目的是为了通过创建“钩子”拦截框架中大部分的组件操作。了解更多

事件列表(Events List)

可以通过返回 false 会阻止应用后续动作并且停止继续下发的事件:

Name Can return value
acl:beforeCheckAccess NO
dispatch:beforeDispatchLoop NO
dispatch:beforeDispatch NO
dispatch:beforeNotFoundAction NO
dispatch:beforeExecuteRoute NO
dispatch:beforeInitialize NO
dispatch:afterInitialize NO
dispatch:beforeException NO
application:boot NO
application:beforeHandleRouter NO
application:afterHandleRouter NO
application:beforeStartModule NO
application:afterStartModule NO
application:beforeHandleRequest NO
application:afterHandleRequest NO
application:beforeRenderView NO
console:beforeStartModule NO
console:afterStartModule NO
console:beforeHandleTask NO
router:beforeHandle Yes
router:afterHandleRouter NO
micro:beforeHandleRoute NO
micro:beforeExecuteRoute NO
micro:beforeNotFound NO
model:beforeValidation NO
model:beforeValidationOnCreate NO
model:beforeValidationOnUpdate NO
model:validation NO
model:afterValidationOnCreate NO
model:afterValidationOnUpdate NO
model:afterValidation NO
model:beforeSave NO
model:beforeUpdate NO
model:beforeCreate NO
model:beforeOperation NO
model:beforeDelete NO
view:beforeRender NO
view:beforeRenderView NO
db:beforeExecutePrepared NO
db:afterExecutePrepared NO
db:beforeQuery NO
db:beforeExecute NO

只能可以通过 stop() 停止的继续下发的事件:

Name Can return value
acl:afterCheckAccess NO
loader:beforeCheckPath NO
loader:pathFound NO
loader:beforeCheckClass NO
loader:afterCheckClass NO
di:beforeServiceResolve NO
di:afterServiceResolve NO
di:afterServiceResolve NO
dispatch:afterExecuteRoute NO
dispatch:afterDispatch NO
dispatch:afterDispatchLoop NO
dispatch:beforeForward NO
dispatch:beforeException NO
application:beforeCheckUseImplicitView NO
application:afterCheckUseImplicitView NO
application:afterRenderView NO
application:beforeSendResponse NO
application:afterSendResponse NO
console:afterHandleTask NO
view:afterRenderView NO
view:notFoundView NO
pagination:beforeGetPaginate NO
pagination:afterGetPaginate Yes
router:beforeCheckRoutes NO
router:beforeCheckRoute NO
router:matchedRoute NO
router:notMatchedRoute NO
router:afterCheckRoutes NO
micro:afterExecuteRoute NO
micro:afterHandleRoute NO
model:beforeQuery YES
model:afterQuery YES
model:notDeleted NO
model:notSaved NO
model:onValidationFails NO
model:afterUpdate NO
model:afterCreate NO
model:afterDelete NO
model:afterSave NO
model:afterOperation NO
model:beforeToArray NO
model:afterToArray YES
modelsManager:beforeInitialize NO
modelsManager:afterInitialize NO
view:afterRender NO
query:beforePrepareSelect NO
query:afterPrepareSelect NO
query:beforePrepareInsert NO
query:afterPrepareInsert NO
query:beforePrepareUpdate NO
query:afterPrepareUpdate NO
query:beforePrepareDelete NO
query:afterPrepareDelete NO
query:beforeParse YES
query:afterParse YES
query:beforeExecuteSelect NO
query:beforeGenerateSQLStatement NO
query:afterGenerateSQLStatement YES
query:afterExecuteSelect NO
query:beforeExecuteInsert NO
query:afterExecuteInsert NO
query:beforeExecuteUpdate NO
query:afterExecuteUpdate NO
query:beforeExecuteDelete NO
query:afterExecuteDelete NO
query:beforeExecute YES
query:afterExecute YES
db:afterQuery NO
db:afterExecute NO
db:beginTransaction NO
db:beforeCreateSavepoint NO
db:afterCreateSavepoint NO
db:beforeRollbackTransaction NO
db:afterRollbackTransaction NO
db:BeforeRollbackSavepoint NO
db:afterRollbackSavepoint NO
db:beforeCommitTransaction NO
db:afterCommitTransaction NO
db:beforeReleaseSavepoint NO
db:afterReleaseSavepoint NO

示例:

  1. <?php
  2.  
  3. $eventsManager = new Phalcon\Events\Manager();
  4.  
  5. $eventsManager->attach('model:beforeQuery', function($event, $model, $data, $prevdata) {
  6. if (!$prevdata) {
  7. return ['data' => 1];
  8. }
  9. return $prevdata;
  10. });
  11.  
  12. $eventsManager->attach('model:beforeQuery', function($event, $model, $data, $prevdata) {
  13. if (!$prevdata) {
  14. return [];
  15. }
  16. $prevdata['data2'] = 2;
  17. return $prevdata;
  18. });
  19.  
  20. $di = new Phalcon\Di\FactoryDefault;
  21. $di->set('modelsManager', function() use ($eventsManager) {
  22. $modelsManager = new Phalcon\Mvc\Model\Manager();
  23. $modelsManager->setEventsManager($eventsManager);
  24. return $modelsManager;
  25. }, true);
  26.  
  27. class Robots extends Phalcon\Mvc\Model {
  28. public function beforeQuery($event, $data, $prevdata) {
  29. if (!$prevdata) {
  30. return [];
  31. }
  32. $prevdata['data3'] = 3;
  33. return $prevdata;
  34. }
  35. }
  36.  
  37. var_dump(Robots::find());

输出:

  1. array(2) {
  2. ["data"]=>
  3. int(1)
  4. ["data2"]=>
  5. int(2)
  6. ["data3"]=>
  7. int(3)
  8. }

原文: http://www.myleftstudio.com/reference/events.html