Documentation

事件(Events)

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

使用示例(Usage Example)

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

<?php

use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;

$connection = new DbAdapter(
    array(
        "host"     => "localhost",
        "username" => "root",
        "password" => "secret",
        "dbname"   => "invo"
    )
);

$connection->attachEvent('beforeExecutePrepare', function($event, $statement, $prevData = NULL){

    });

$connection->query("SELECT * FROM products p WHERE p.status = 1");

事件的执行

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

<?php

$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

示例:

<?php

$eventsManager = new Phalcon\Events\Manager();

$eventsManager->attach('model:beforeQuery', function($event, $model, $data, $prevdata) {
        if (!$prevdata) {
            return ['data' => 1];
    }
    return $prevdata;
});

$eventsManager->attach('model:beforeQuery', function($event, $model, $data, $prevdata) {
    if (!$prevdata) {
        return [];
    }
    $prevdata['data2'] = 2;
    return $prevdata;
});

$di = new Phalcon\Di\FactoryDefault;
$di->set('modelsManager', function() use ($eventsManager) {
    $modelsManager = new Phalcon\Mvc\Model\Manager();
    $modelsManager->setEventsManager($eventsManager);
    return $modelsManager;
}, true);

class Robots extends Phalcon\Mvc\Model {
    public function beforeQuery($event, $data, $prevdata) {
        if (!$prevdata) {
            return [];
        }
        $prevdata['data3'] = 3;
        return $prevdata;
    }
}

var_dump(Robots::find());

输出:

array(2) {
    ["data"]=>
    int(1)
    ["data2"]=>
    int(2)
    ["data3"]=>
    int(3)
}