承接 claudye/laravel-hooks 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

claudye/laravel-hooks

Composer 安装命令:

composer require claudye/laravel-hooks

包简介

A Laravel Trait for Hooking Before and After Method Calls

README 文档

README

Laravel Hooks is a Laravel trait that allows you to register hooks (callbacks) before and after calling specified methods in a Laravel controllers. This helps you modify or extend the behavior of methods without altering their core logic.

1. Features and Usage

Key Features:

  • Before and After Hooks: Easily register callbacks that are executed before or after specific controller (action) methods.
  • Non-intrusive Method Modification: Extend or modify method behaviors without touching the core code.
  • Automatic Hook Setup: Use hooks automatically by leveraging a predefined useHooks method.

Use Cases:

  • Logging: Automatically log actions before or after certain methods are executed.
  • Data Validation: Add complex validation logic before the execution of critical methods.
  • Response Transformation: Modify or format a method’s return value after it’s called.
  • Controller Instance Modification: Modify the controller instance before a method is executed, useful when combining with dependency injection.
  • Cross-cutting Concerns: Apply logic across multiple methods like auditing or authorization without duplicating code.

2. Installation

You can install this package via Composer. Make sure your Laravel version is 7.x or higher (up to 11.x supported).

Run the following command to install the package:

composer require claudye/laravel-hooks

3. Example of Usage

Here's an example of how to use the HasControllerHooks trait in your Laravel controller or class:

namespace App\Http\Controllers;

use LaravelHooks\Traits\HasControllerHooks;
use Illuminate\Http\Request;

class ExampleController extends Controller
{
    use HasControllerHooks;

    public function index(Request $request)
    {
        // Your logic here.
        return [
            "users" => $this->queryBuilder->all()
        ];
    }

    public function show(Request $request)
    {
        // Your logic here.
        return [
            "user" => $user
        ];
    }

    /**
     * Defines hooks for methods in the controller.
     */
    public function useHooks()
    {
        // Register a before hook for the 'index' method
        $this->beforeCalling(['index', 'show'], function ($request,...$parameters, $method) {
            
            $this->queryBuilder->filters($request->all()); 

            logger('Before calling index method');
        });

        // Register an after hook for the 'index' method
        $this->afterCalling(['index',"show","edit"], function ($request, $result,...$parameters, $method) {

            // Modify the result after the 'index' method is called
            logger('After calling index method');

            event(new SomeEvent($result))

            return response()->json([
                'data'=>$result
            ]);
        });

        // You can add other hooks as necessary.
    }
}

Advanced Usage

1. Modifying Controller Instance Before Method Execution

You can modify the controller instance itself before a method is executed, which is particularly useful when working with dependency injection.

Example:

$this->beforeCalling(['store',"update","delete"], function ($parameter1, $parameter2,...$methodName) {
    // Modify the controller instance before executing 'store' method
    $this->someService = app(SomeService::class)->init();

    $parameter1->modify();
});

Error Handling

If a method does not exist when trying to call it via the callAction method, a BadMethodCallException will be thrown, providing more clarity during development.

4. Requirements

  • Laravel 7.x to 11.x

5. License

This package is open-source software licensed under the MIT License.

claudye/laravel-hooks 适用场景与选型建议

claudye/laravel-hooks 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.04k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2024 年 09 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 claudye/laravel-hooks 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 claudye/laravel-hooks 我们能提供哪些服务?
定制开发 / 二次开发

基于 claudye/laravel-hooks 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2024-09-20