PHP Conference Japan 2024

Yaf_Plugin_Abstract 類別

(Yaf >=1.0.0)

簡介

插件允許輕鬆擴展和客製化框架。

插件是類別。實際的類別定義會根據組件而有所不同——您可能需要實作此介面,但事實仍然是插件本身是一個類別。

可以使用 Yaf_Dispatcher::registerPlugin() 將插件載入 Yaf,註冊後,插件根據此介面實作的所有方法都將在適當的時間被呼叫。

範例

範例 #1 插件範例

<?php
/* bootstrap class should be defined under ./application/Bootstrap.php */
class Bootstrap extends Yaf_Bootstrap_Abstract {
public function
_initPlugin(Yaf_Dispatcher $dispatcher) {
/* register a plugin */
$dispatcher->registerPlugin(new TestPlugin());
}
}

/* plugin class should be placed under ./application/plugins/ */
class TestPlugin extends Yaf_Plugin_Abstract {
public function
routerStartup(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {
/* before router
in this hook, user can do some url rewrite */
var_dump("routerStartup");
}
public function
routerShutdown(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {
/* router complete
in this hook, user can do login check */
var_dump("routerShutdown");
}
public function
dispatchLoopStartup(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {
var_dump("dispatchLoopStartup");
}
public function
preDispatch(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {
var_dump("preDispatch");
}
public function
postDispatch(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {
var_dump("postDispatch");
}
public function
dispatchLoopShutdown(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {
/* final hook
in this hook user can do logging or implement layout */
var_dump("dispatchLoopShutdown");
}
}

Class
IndexController extends Yaf_Controller_Abstract {
public function
indexAction() {
return
FALSE; //prevent rendering
}
}

$config = array(
"application" => array(
"directory" => dirname(__FILE__) . "/application/",
),
);

$app = new Yaf_Application($config);
$app->bootstrap()->run();
?>

以上範例將輸出類似以下的內容

string(13) "routerStartup"
string(14) "routerShutdown"
string(19) "dispatchLoopStartup"
string(11) "preDispatch"
string(12) "postDispatch"
string(20) "dispatchLoopShutdown"

類別概要

目錄

新增註釋

使用者貢獻的註釋 1 則註釋

gianjason#gmail.com
11 年前
所有根據此介面實作的插件方法,都會在適當的時間自動被呼叫。
To Top