You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Oxwall平台EventManager绑定、触发机制及示例技术咨询

Hey there! Let me walk you through how Oxwall's EventManager handles registering custom methods to specific events, plus share practical examples for binding listeners and triggering events.

Oxwall EventManager: Core Mechanism

Oxwall’s EventManager (powered by the OW_EventManager class) follows the observer pattern—it lets components "subscribe" to events, and automatically runs all registered callback methods when that event is triggered. This is how plugins/themes hook into core or custom workflows without modifying core code directly.

Key Terms to Know

  • Event Name: A unique string (e.g., "user.register" or "myplugin.post_created") that identifies when your listener should run.
  • Listener: A callback (class method or closure) that executes when the event fires.
  • Event Data: An array of values passed to listeners when the event is triggered (e.g., user ID, post content).

Step 1: Binding a Listener to an Event

You’ll typically set up event bindings in your plugin’s init() method or a dedicated event handler class. First, grab the EventManager instance, then use bind() to attach your listener.

Example 1: Bind a Class Method

Let’s create a listener for a custom "myplugin.post_created" event:

// Get the global EventManager instance
$eventManager = OW::getEventManager();

// Bind a static class method as the listener
$eventManager->bind("myplugin.post_created", ["MYPLUGIN_EventHandler", "onPostCreated"]);

Here’s what the corresponding event handler class (MYPLUGIN_EventHandler.php) might look like:

class MYPLUGIN_EventHandler
{
    public static function onPostCreated(OW_Event $event)
    {
        // Extract data passed with the event
        $eventData = $event->getData();
        $postId = $eventData["postId"];
        $userId = $eventData["userId"];

        // Add your custom logic here—e.g., send a notification
        OW::getNotificationService()->addNotification(
            $userId,
            $userId,
            "myplugin",
            "new_post_alert",
            ["postId" => $postId]
        );
    }
}

Example 2: Bind an Inline Closure

For simpler logic, you can use a closure directly:

$eventManager->bind("myplugin.post_created", function(OW_Event $event) {
    $data = $event->getData();
    // Log the new post to the server error log
    error_log("New post published: ID " . $data["postId"] . " by User " . $data["userId"]);
});

Step 2: Triggering an Event

When the relevant action happens (e.g., a user submits a post), you need to trigger the event to run all registered listeners. Use trigger() with an OW_Event object containing your event data.

Example: Trigger the Custom Post Event

// After saving a new post to the database
$newPostData = [
    "postId" => $savedPost->id,
    "userId" => OW::getUser()->getId(),
    "content" => $savedPost->content
];

// Fire the event with your data
OW::getEventManager()->trigger(new OW_Event("myplugin.post_created", $newPostData));

Bonus: Hook Into Oxwall Core Events

Oxwall has dozens of built-in core events you can tap into. For example, here’s how to run code when a new user registers:

$eventManager->bind("user.register", function(OW_Event $event) {
    $userData = $event->getData();
    $newUserId = $userData["userId"];
    
    // Fetch the new user and send a welcome email
    $user = BOL_UserService::getInstance()->findUserById($newUserId);
    OW::getMailService()->sendMail(
        $user->email,
        "Welcome to Our Community!",
        "Hi " . $user->username . ", thanks for joining—we’re glad you’re here!"
    );
});

Pro Tips

  • Priority Control: Add a third parameter to bind() to set listener priority (default is 10). Higher values run first:
    // This listener runs before others with default priority
    $eventManager->bind("user.register", ["MYPLUGIN_EventHandler", "onUserRegister"], 5);
    
  • Modify Event Data: Listeners can update event data for subsequent listeners using $event->setData($modifiedData).

内容的提问来源于stack exchange,提问作者mehrdadep

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:21:14