tadcms / hooks
在 Laravel 中添加动作和过滤器,就像 WordPress 一样。
v1.0.4
2021-05-08 07:00 UTC
Requires
- php: >=7.0
- illuminate/support: >=5.3
- opis/closure: ^3.5
Requires (Dev)
- php-coveralls/php-coveralls: ^2.1
- phpunit/phpunit: ^8.0
This package is auto-updated.
Last update: 2024-09-11 19:16:44 UTC
README
在 Laravel 中添加动作和过滤器,就像 WordPress 一样。
安装
- 使用 Composer 安装
composer require tadcms/hooks
如果你使用的是 Laravel 5.5 或更高版本,你现在可以开始使用这个包了。Eventy 会自动由 Laravel 框架发现。
- 对于 Laravel < 5.5,将服务提供者添加到你的
config/app.php
文件中的 providers 数组。
'Tadcms\Hooks\HooksServiceProvider',
使用方法
在你的代码的任何地方,你可以创建一个新的动作,如下所示
do_action($tag, ...$args)
* TAD CMS: Do action hook
* @param string $tag Action / Hook name (E.x: my.hook)
* @param mixed ...$args Additional parameters to pass to the callback functions.
* @return void
添加动作 为了监听你的钩子,你添加监听器。这些最好添加到你的 AppServiceProvider boot()
方法中。
add_action($tag, $callback, $priority = 20, $arguments = 1)
* Add action to hook
* @param string $tag The name of the filter to hook the $function_to_add callback to.
* @param callable $callback The callback to be run when the filter is applied.
* @param int $priority Optional. Used to specify the order in which the functions
* associated with a particular action are executed.
* Lower numbers correspond with earlier execution,
* and functions with the same priority are executed
* in the order in which they were added to the action. Default 20.
* @param int $arguments Optional. The number of arguments the function accepts. Default 1.
* @return void
应用过滤器
apply_filters($tag, $value, ...$args)
* TAD CMS: Apply filters to value
* @param string $tag The name of the filter hook.
* @param mixed $value The value to filter.
* @param mixed ...$args Additional parameters to pass to the callback functions.
* @return mixed The filtered value after all hooked functions are applied to it.
添加过滤器 为了监听你的钩子,你添加监听器。这些最好添加到你的 AppServiceProvider boot()
方法中。
add_filters($tag, $callback, $priority = 20, $arguments = 1)
* @param string $tag The name of the filter to hook the $function_to_add callback to.
* @param callable $callback The callback to be run when the filter is applied.
* @param int $priority Optional. Used to specify the order in which the functions
* associated with a particular action are executed.
* Lower numbers correspond with earlier execution,
* and functions with the same priority are executed
* in the order in which they were added to the action. Default 20.
* @param int $arguments Optional. The number of arguments the function accepts. Default 1.
* @return bool
在 Blade 中使用
添加与上面的动作示例相同的动作
@do_action('my.hook', $user)
添加与上面的过滤器示例相同的过滤器
You are @apply_filters('my.hook', 'awesome')