PHPackages                             claudye/laravel-hooks - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. claudye/laravel-hooks

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

claudye/laravel-hooks
=====================

A Laravel Trait for Hooking Before and After Method Calls

1.1.1(1y ago)91.0kPHP

Since Sep 20Pushed 1y ago1 watchersCompare

[ Source](https://github.com/Claudye/laravel-hooks)[ Packagist](https://packagist.org/packages/claudye/laravel-hooks)[ RSS](/packages/claudye-laravel-hooks/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (1)Versions (4)Used By (0)

Laravel Hooks
=============

[](#laravel-hooks)

`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
---------------------

[](#1-features-and-usage)

### Key Features:

[](#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:

[](#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
---------------

[](#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
-------------------

[](#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

[](#advanced-usage)

#### 1. Modifying Controller Instance Before Method Execution

[](#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

[](#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
---------------

[](#4-requirements)

- Laravel 7.x to 11.x

5. License
----------

[](#5-license)

This package is open-source software licensed under the [MIT License](LICENSE).

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~24 days

Total

3

Last Release

549d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/d7d8e8fcdd2c322c0ca54bc495bced6c7bf9b8a7296abfbae4e5ad0ae0834de9?d=identicon)[Claudy](/maintainers/Claudy)

---

Top Contributors

[![Claudye](https://avatars.githubusercontent.com/u/73280595?v=4)](https://github.com/Claudye "Claudye (5 commits)")

### Embed Badge

![Health badge](/badges/claudye-laravel-hooks/health.svg)

```
[![Health](https://phpackages.com/badges/claudye-laravel-hooks/health.svg)](https://phpackages.com/packages/claudye-laravel-hooks)
```

###  Alternatives

[wireui/wireui

TallStack components

1.8k1.3M16](/packages/wireui-wireui)[livewire/volt

An elegantly crafted functional API for Laravel Livewire.

4195.3M84](/packages/livewire-volt)[ramonrietdijk/livewire-tables

Dynamic tables for models with Laravel Livewire

21147.4k](/packages/ramonrietdijk-livewire-tables)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
