PHPackages                             magdicom/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. magdicom/laravel-hooks

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

magdicom/laravel-hooks
======================

A lightweight action hooks package for Laravel

v1.1.0(3y ago)5262[1 issues](https://github.com/magdicom/laravel-hooks/issues)[3 PRs](https://github.com/magdicom/laravel-hooks/pulls)MITPHPPHP ^8.0

Since Dec 31Pushed 2y ago1 watchersCompare

[ Source](https://github.com/magdicom/laravel-hooks)[ Packagist](https://packagist.org/packages/magdicom/laravel-hooks)[ Docs](https://github.com/magdicom/laravel-hooks)[ GitHub Sponsors](https://github.com/magdicom)[ RSS](/packages/magdicom-laravel-hooks/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (3)Dependencies (12)Versions (7)Used By (0)

A lightweight action hooks package for Laravel
==============================================

[](#a-lightweight-action-hooks-package-for-laravel)

[![Latest Version on Packagist](https://camo.githubusercontent.com/c0929010b05f9df07f87dd78a7896eb270457f99b8990f20ce5c8d87635323dd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d61676469636f6d2f6c61726176656c2d686f6f6b732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/magdicom/laravel-hooks)

This is a laravel 9 [magdicom/hooks](https://github.com/magdicom/hooks) wrapper that lets easily use action hooks in your laravel project.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require magdicom/laravel-hooks
```

Usage
-----

[](#usage)

To call any of the available method you can use the facade:

```
use Magdicom\LaravelHooks\Facade\Hooks;

Hooks::register("HookName", function($vars){}, 1);
```

or use the helper function as below:

```
hooks()->register("HookName", function($vars){}, 1);
```

### Quick Start

[](#quick-start)

```
# Register our functions
Hooks::register("Greetings", function($vars){
    return "Hi There,";
}, 1);

Hooks::register("Greetings", function($vars){
    return "This is the second line of greetings!";
}, 2);

# Later we run it
echo Hooks::all("Greetings")->toString("");
```

The above example will output

```
Hi There,
This is the second line of greetings!

```

### Output

[](#output)

When you call any of [`all`](#methods-all), [`first`](#methods-first) or [`last`](#methods-last) methods, the corresponding hook functions will be executed and their output will be saved in a special property to be exported later using [`toString`](#methods-tostring) or [`toArray`](#methods-toarray) methods.

### Callbacks

[](#callbacks)

#### Closure

[](#closure)

```
Hooks::register("Callback", function($vars) {
    return "Closure";
});
```

#### Function Name

[](#function-name)

```
function simple_function_name($vars){
    //
}

Hooks::register("Callback", "simple_function_name");
```

#### Object Method

[](#object-method)

```
class FooBar {
    public function methodName($vars){
        //
    }
}

$object = new FooBar;

Hooks::register("Callback", [$object, 'methodName']);
```

or

```
Hooks::register("Callback", [(new FooBar), 'methodName']);
```

#### Static Method

[](#static-method)

```
class FooBar {
    public static function staticMethodName($vars){
        //
    }
}

Hooks::register("Callback", ['FooBar', 'staticMethodName']);
```

in case this is not a static method, an object will be created and the provided method will be called.

### Parameters

[](#parameters)

With each of hook callback functions execution an array of parameters could be passed to it to help it perform the required action.

Parameters split into two types:

- Global parameters will be available across all hook names and callbacks, and these can be defined using [`setParameter`](#methods-setparameter) and [`setParameters`](#methods-setparameters) methods.
- Scoped parameters which will be only available to the requested hook name, and could be provided as the second argument of [`all`](#methods-all), [`first`](#methods-first) and [`last`](#methods-last) methods.

### Priority

[](#priority)

When you need to ensure that certain hook functions should be executed in sequence order, here it comes `$priority` which is the 3rd and last argument of [`register`](#methods-register) method.

### Methods

[](#methods)

#### register

[](#register)

```
Hooks::register(string $hookName, array|callable $callback, ?int $priority): self
```

Register all your hook functions via this method:

- `$hookName` this can be anything you want, its like a group name where all other related action hook functions will be attached to.
- `$callback` only accepts [callable](https://www.php.net/manual/en/language.types.callable.php) functions.
- `$priority` (optional) used to sort callbacks before being executed.

#### all

[](#all)

```
Hooks::all(string $hookName, ?array $parameters): self
```

Will execute all callback functions of the specified hook name, by default it will return the output as string, check [output](#output) section for more options.

- `$hookName` the hook name you want to execute its callback functions.
- `$parameters` optional key, value pair array that you want to provide for all callback functions related to the same hook name.

Please Note: parameters provided via this method will be available only in the scope of the specified hook name, to specify global parameters use [`setParameter`](#methods-setparameter), [`setParameters`](#methods-setparameters) methods instead.

#### first

[](#first)

```
Hooks::first(string $hookName, ?array $parameters): self
```

Similar to [`all`](#methods-all) method in every aspect with the exception that only the first callback (after sorting) will be executed.

#### last

[](#last)

```
Hooks::last(string $hookName, ?array $parameters): self
```

Similar to [`all`](#methods-all) method in every aspect with the exception that only the last callback (after sorting) will be executed.

#### toArray

[](#toarray)

```
Hooks::toArray(): array
```

Will return output of the last executed hook name functions as an array.

#### toString

[](#tostring)

```
Hooks::toString(?string $separator): string
```

Will return output of the last executed hook name functions as one string.

- `$separator` could be used to separate the output as you need (e.g: "\\n", "&lt;br&gt;").

#### setParameter

[](#setparameter)

```
Hooks::setParameter(string $name, mixed $value): self
```

Use this method to define a parameter that will be accessible from any hook function.

- `$name` name of the parameter.
- `$value` value of the parameter could be string, array or even an object.

P.S: if the parameter already defined then its old value will be replaced by the value provided here.

#### setParameters

[](#setparameters)

```
Hooks::setParameters(array $parameters): self
```

Same as [`setParameter`](#methods-setparameter) but here it accepts a name, value pair array as its only argument.

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Mohamed Magdi](https://github.com/magdicom)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance16

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

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 ~194 days

Total

3

Last Release

1204d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/40920709?v=4)[momagdi](/maintainers/momagdi)[@momagdi](https://github.com/momagdi)

---

Top Contributors

[![magdicom](https://avatars.githubusercontent.com/u/91500450?v=4)](https://github.com/magdicom "magdicom (15 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (11 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (11 commits)")

---

Tags

laravelLaravel hooksaction-hooksmagdicom

###  Code Quality

TestsPest

### Embed Badge

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

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

###  Alternatives

[spatie/laravel-data

Create unified resources and data transfer objects

1.7k28.9M627](/packages/spatie-laravel-data)[hirethunk/verbs

An event sourcing package that feels nice.

513162.9k6](/packages/hirethunk-verbs)[worksome/exchange

Check Exchange Rates for any currency in Laravel.

123544.7k](/packages/worksome-exchange)[ralphjsmit/livewire-urls

Get the previous and current url in Livewire.

82270.3k4](/packages/ralphjsmit-livewire-urls)[hydrat/filament-table-layout-toggle

Filament plugin adding a toggle button to tables, allowing user to switch between Grid and Table layouts.

6292.3k1](/packages/hydrat-filament-table-layout-toggle)[ralphjsmit/laravel-helpers

A package containing handy helpers for your Laravel-application.

13704.6k2](/packages/ralphjsmit-laravel-helpers)

PHPackages © 2026

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