定制 creativesofttechsolutions/laravelhooks 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

creativesofttechsolutions/laravelhooks

最新稳定版本:v1.0.1

Composer 安装命令:

composer require creativesofttechsolutions/laravelhooks

包简介

Wordpress Like Hooks System Package For Laravel

README 文档

README

This documentation explains how to build and use a flexible and performant hook system in Laravel 12. It supports both actions and filters, similar to WordPress, and works across controllers, Livewire components, middleware, and modules.

🛠 Installation

To install the creativesofttechsolutions/laravelhooks package, use Composer:

composer require creativesofttechsolutions/laravelhooks

🧠 What Are Actions and Filters?

🔹 Actions

  • Actions are event triggers.
  • They allow you to run additional code when something happens (e.g., after user registration, after an order is placed).
  • Actions do not modify any data — they just perform side effects.
hooks()->addAction('after_user_register', function ($user) {
    Log::info("New user: " . $user->email);
});

🔸 Filters

  • Filters are used to modify and return data.
  • They allow other parts of the application or plugins to intercept, change, or validate values before they are used.
hooks()->addFilter('post_title', function ($title) {
    return strtoupper($title);
});

🚀 Usage Examples

Triggering an action:

hooks()->doAction('after_user_register', $user);

Adding an action (e.g., in a module):

hooks()->addAction('after_user_register', function ($user) {
    Log::info("User Registered: " . $user->email);
});

Adding and using filters:

hooks()->addFilter('product_price', function ($price, $product) {
    return $product->discount ? $price * 0.9 : $price;
});

$price = hooks()->applyFilters('product_price', $originalPrice, $product);

📦 Where to Define Hooks in Modules

In Modules/YourModule/Providers/ModuleServiceProvider.php:

public function boot(): void {
    hooks()->addAction('after_user_register', ...);
    hooks()->addFilter('modify_data', ...);
}

Or in a dedicated class like:

Modules/YourModule/Hooks/HookRegistrar.php

And load them like:

HookRegistrar::register();

⚙️ Using Hooks in Controllers, Livewire, Middleware

In Controllers:

hooks()->doAction('after_order_created', $order);

In Livewire Components:

$this->value = hooks()->applyFilters('modify_value', $this->value);

In Middleware:

$request = hooks()->applyFilters('modify_request', $request);

✅ Best Practices

  • Use meaningful, unique hook names.
  • Actions for side effects, Filters for modifying data.
  • Use classes instead of closures for better readability and testing.
  • Avoid heavy operations inside hook callbacks.

This system brings WordPress-style flexibility to Laravel while preserving Laravel’s clean architecture. You can dynamically extend your app and let plugins interact with core logic cleanly.

统计信息

  • 总下载量: 11
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-06-14