PHPackages                             ahmetertem/hook - 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. ahmetertem/hook

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

ahmetertem/hook
===============

Simple hook engine for Laravel

071PHP

Since May 17Pushed 1y agoCompare

[ Source](https://github.com/ahmetertem/Hook)[ Packagist](https://packagist.org/packages/ahmetertem/hook)[ RSS](/packages/ahmetertem-hook/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependenciesVersions (1)Used By (0)

Hook engine for Laravel
=======================

[](#hook-engine-for-laravel)

**Namespace changed?**

I had to change to namespace from Esemve to mine because in that case I've couldn't load it from Illuminate' reflector. Original author is Esembe.

**What is this?**

The purpose of this project is that your packages could modify each other without overriding the source code.

**What is a Hook?**

It is similar to an event. A code bounded by a hook runs unless a hook listener catches it and orders that instead of the function in the hook, something else should run. They could be set in an established order, so you are able to make several modifications in the code.

**What is it good for?**

Example 1: You have a module which displays an editor. This remains the same editor in every case. If you bound the display of the editor in a hook, then you can write a module which can redefine/override this hook, and for example changs the textarea to a ckeditor.

Example 2: You list the users. You can include every line's print in a hook. This way you can write a separate module which could extend this line with an e-mail address print.

Example 3: You save the users' data in a database. If you do it in a hook, you can write a module which could add extra fields to the user model like "first name" or "last name". To do that, you didn't need to modify the code that handles the users, the extension module doesn't need to know the functioning of the main module.

... and so many other things. If you are building a CMS-like system, it will make your life a lot easier.

How do I install it?
====================

[](#how-do-i-install-it)

```
composer require ahmetertem/hook
```

then to the app.php :

```
...
'providers' => [
    ...
    AhmetErtem\Hook\HookServiceProvider::class,
    ...
 ],
 'aliases' =>[
    ...
    'Hook' => AhmetErtem\Hook\Facades\Hook::class
    ...
 ]
```

How does it work?
=================

[](#how-does-it-work)

Example:

```
$user = new User();
$user = Hook::get('fillUser',[$user],function($user){
    return $user;
});
```

In this case a fillUser hook is thrown, which receive the $user object as a parameter. If nothing catches it, the internal function, the return $user will run, so nothing happens. But it can be caught by a listener from a provider:

```
Hook::listen('fillUser', function ($callback, $output, $user) {
    if (empty($output))
    {
      $output = $user;
    }
    $output->profilImage = ProfilImage::getForUser($user->id);
    return $output;
}, 10);
```

The $callback contains the hook's original internal function, so it can be called here.

Multiple listeners could be registered to a hook, so in the $output the listener receives the response of the previously registered listeners of the hook.

THen come the parameters delivered by the hook, in this case the user.

The hook listener above caught the call of the fillUser, extended the received object, and returned it to its original place. After the run of the hook the $user object contains a profilImage variable as well.

Number 10 in the example is the priority. They are executed in an order, so if a number 5 is registered to the fillUser as well, it will run before number 10.

Initial output
==============

[](#initial-output)

You can pass initial output to the listeners too.

```
$initialOutput='test string';

\Hook::get('testing',['other string'],function($otherString){
    return $otherString;
},$initialOutput)

// and later ...

Hook::listen('testing', function ($callback, $output, $otherString) {
    if ($output==='test string') {
        $output="{$output} yeeeaaaayyy!";
    }
    if ($otherString==='other_string') {
        // other string is good too
    }
    return $output; // 'test string yeeeaaaayyy!'
});
```

If there is no listeners, 'other string' will be returned.

Usage in blade templates
========================

[](#usage-in-blade-templates)

```
@hook('hookName')
```

In this case the hook listener can catch it like this:

```
 Hook::listen('template.hookName', function ($callback, $output, $variables) {
   return view('test.button');
 });
```

In the $variables variable it receives all of the variables that are available for the blade template.

❗ **To listen blade templates you need to listen `template.hookName` instead of just `hookName`!**

Wrap HTML
=========

[](#wrap-html)

```
@hook('hookName', true)
    this content can be modified with dom parsers
    you can inject some html here
@endhook
```

Now the `$output` parameter contains html wrapped by hook component.

```
Hook::listen('template.hookName', function ($callback, $output, $variables) {
  return "$output";
});
```

Stop
====

[](#stop)

```
Hook::stop();
```

Put in a hook listener it stops the running of the other listeners that are registered to this hook.

For testing
===========

[](#for-testing)

```
Hook::mock('hookName','returnValue');
```

After that the hookName hook will return returnValue as a response.

Artisan
=======

[](#artisan)

```
php artisan hook::list
```

Lists all the active hook listeners.

---

License: MIT

###  Health Score

17

—

LowBetter than 6% of packages

Maintenance35

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity15

Early-stage or recently created project

 Bus Factor1

Top contributor holds 50% 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/549875e8ebf02b54a3da977ab681b0004a2e85f7fe8f6f3da75bcf208002a5a3?d=identicon)[ahmetertem](/maintainers/ahmetertem)

---

Top Contributors

[![esemve](https://avatars.githubusercontent.com/u/7847061?v=4)](https://github.com/esemve "esemve (12 commits)")[![ahmetertem](https://avatars.githubusercontent.com/u/155022?v=4)](https://github.com/ahmetertem "ahmetertem (5 commits)")[![stephane-monnot](https://avatars.githubusercontent.com/u/6066368?v=4)](https://github.com/stephane-monnot "stephane-monnot (3 commits)")[![Xulai](https://avatars.githubusercontent.com/u/5481226?v=4)](https://github.com/Xulai "Xulai (3 commits)")[![vroomfondle](https://avatars.githubusercontent.com/u/6186584?v=4)](https://github.com/vroomfondle "vroomfondle (1 commits)")

### Embed Badge

![Health badge](/badges/ahmetertem-hook/health.svg)

```
[![Health](https://phpackages.com/badges/ahmetertem-hook/health.svg)](https://phpackages.com/packages/ahmetertem-hook)
```

###  Alternatives

[kongulov/nova-tab-translatable

Making Nova Tab Translatable

8562.2k3](/packages/kongulov-nova-tab-translatable)[adigital/gdprdatachecker

Run through the database and pull out any information associated with a specified email address

151.0k](/packages/adigital-gdprdatachecker)

PHPackages © 2026

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