> For the complete documentation index, see [llms.txt](https://quantumphp.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://quantumphp.gitbook.io/docs/packages/hook/usage.md).

# Usage

## Register hook names first

Hook names come from `config/hooks`, so define them before application code starts attaching listeners.

```php
return [
    'user.registered',
    'mail.sent',
];
```

The package expects a flat list of names. Duplicate entries stop `HookManager` during construction.

## Queue a listener and fire it once

```php
hook()->on('user.registered', function (?array $args): void {
    $userId = $args['user_id'] ?? null;

    // react to the event
});

hook()->fire('user.registered', [
    'user_id' => 42,
]);
```

`fire()` passes the full argument array as a single parameter to the listener.

## The same hook is empty after firing

Listeners are removed as they are executed.

```php
hook()->on('user.registered', function (?array $args): void {
    // runs once
});

hook()->fire('user.registered');
hook()->fire('user.registered'); // no queued listeners remain
```

If you need the listener again, queue it again with `on()`.

## Unregistered hook names fail

Both `on()` and `fire()` require a hook name that was registered during manager construction.

```php
hook()->on('missing-hook', function (?array $args): void {
    // never reached
});
```

This raises `HookException` because the name does not exist in the internal registry.

## Listener signature expectations

Every listener is invoked as `$fn($args)`.

That means the safest listener shape is a callable that accepts one parameter, usually `?array $args`.

A zero-argument listener is not compatible with how `fire()` invokes callables.

## Inspect current manager state

```php
$hooks = hook()->getRegistered();
```

The returned array includes:

* every registered hook name
* the currently queued listeners for each hook

This is useful for debugging whether a hook exists and whether listeners are still waiting to run.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://quantumphp.gitbook.io/docs/packages/hook/usage.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
