Skip to content

Commit 4cdfa6a

Browse files
committed
Update README.md: enhance documentation for hooks, actions, and filters
1 parent c74fab8 commit 4cdfa6a

File tree

1 file changed

+32
-20
lines changed

1 file changed

+32
-20
lines changed

README.md

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
[![Latest Version on Packagist](https://img.shields.io/packagist/v/saschaende/laravel-hookable.svg?style=flat-square)](https://packagist.org/packages/saschaende/laravel-hookable)
44

5-
Laravel Hookable is a package that allows you to easily add hooks (actions and filters) to your Laravel applications,
6-
similar to the hook system in WordPress.
5+
Laravel Hookable is a package that allows you to use hooks (actions and filters) in Laravel applications – similar to the hook system in WordPress.
76

87
## Installation
98

@@ -13,59 +12,73 @@ You can install the package via Composer:
1312
composer require saschaende/laravel-hookable
1413
```
1514

16-
## Usage
15+
The package is automatically registered by Laravel (see `composer.json`).
16+
17+
## How it works
1718

18-
### Adding Hooks
19+
- **Actions**: Execute code without modifying data (e.g., for events or extensions).
20+
- **Filters**: Allow modification of data.
1921

20-
You can add hooks using the `Hookable` facade. There are two types of hooks: actions and filters.
22+
The package provides a `Hookable` facade that exposes the main methods. The binding is handled automatically via the ServiceProvider.
2123

22-
#### Actions
24+
## Usage
2325

24-
Actions are hooks that do not modify data. You can add an action like this:
26+
### Adding actions
2527

2628
```php
2729
use SaschaEnde\Hookable\Facades\Hookable;
30+
2831
Hookable::action('my_action', function ($arg1, $arg2) {
2932
// Do something with $arg1 and $arg2
3033
});
3134
```
3235

33-
You can then trigger the action like this:
36+
To execute the action (e.g., at a specific point in your code):
3437

3538
```php
36-
Hookable::action('my_action', $arg1, $arg2);
39+
Hookable::renderActions('my_action', $arg1, $arg2);
3740
```
3841

39-
#### Filters
40-
41-
Filters are hooks that modify data. You can add a filter like this:
42+
### Adding filters
4243

4344
```php
4445
use SaschaEnde\Hookable\Facades\Hookable;
46+
4547
Hookable::filter('my_filter', function ($value) {
4648
// Modify $value
4749
return $value;
4850
});
4951
```
5052

51-
You can then apply the filter like this:
53+
Apply the filter:
5254

5355
```php
5456
$value = Hookable::applyFilters('my_filter', $value);
5557
```
5658

57-
### Priorities and Arguments
59+
### Priorities and arguments
5860

59-
You can specify the priority and number of arguments for your hooks.
61+
You can specify the priority and the number of arguments for hooks:
6062

6163
```php
62-
Hookable::filter('my_action', function ($arg1, $arg2) {
64+
Hookable::action('my_action', function ($arg1, $arg2) {
6365
// Do something
6466
}, 20);
6567
```
6668

67-
In this example, the action will be executed with a priority of 20 and will receive
68-
2 arguments.
69+
In this example, the action will be executed with priority 20 and will receive 2 arguments.
70+
71+
## Blade Directives
72+
73+
The package registers two Blade directives to use hooks directly in Blade templates:
74+
75+
```blade
76+
@applyFilter('my_filter', $value)
77+
@doAction('my_action', $arg1, $arg2)
78+
```
79+
80+
- `@applyFilter('filter_name', $value)`: Applies a filter to a value and outputs the result.
81+
- `@doAction('action_name', ...)`: Executes an action and outputs its result.
6982

7083
## Testing
7184

@@ -81,5 +94,4 @@ Contributions are welcome! Please submit a pull request or open an issue on GitH
8194

8295
## License
8396

84-
This package is open-sourced software licensed under the MIT license.
85-
97+
This package is open-source software licensed under the MIT license.

0 commit comments

Comments
 (0)