承接 yanselmask/laravel-hooks 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

yanselmask/laravel-hooks

Composer 安装命令:

composer require yanselmask/laravel-hooks

包简介

The WordPress filter, action system in Laravel

README 文档

README

The WordPress filters, actions system in Laravel.

About

  • An action interrupts the code flow to do something, and then returns back to the normal flow without modifying anything.
  • A filter is used to modify something in a specific way so that the modification is then used by code later on.

Read more about filters and actions

Hooks are a way for one piece of code to interact/modify another piece of code at specific, pre-defined spots.

Installation

composer require yanselmask/laravel-hooks

DONE! Now you can use laravel-hooks

Functions

do_action(  string $tag,  mixed $arg )

This function invokes all functions attached to action hook $tag. It is possible to create new action hooks by simply calling this function, specifying the name of the new hook using the $tag parameter.

Parameters

  • $tag (string) (Required) The name of the action to be executed.

  • $arg (mixed) (Optional) Additional arguments which are passed on to the functions hooked to the action. Default empty.

add_action( string $tag, callable $callback, int $priority = 10, int $accepted_args = 1 )

Actions are the hooks that the Laravel application launches at specific points during execution, or when specific events occur.

Parameters

  • $tag (string) (Required) The name of the action to add the callback to.

  • $callback (callable) (Required) The callback to be run when the action is called.

  • $priority (int) (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 value: 10

  • $accepted_args (int) (Optional) The number of arguments the function accepts. Default value: 1

apply_filters( string $tag, mixed $value )

This function invokes all functions attached to filter hook $tag. It is possible to create new filter hooks by simply calling this function, specifying the name of the new hook using the $tag parameter.

Parameters

  • $tag (string) (Required) The name of the filter hook.

  • $value (mixed) (Required) The value to filter.

add_filter( string $tag, callable $callback, int $priority = 10, int $accepted_args = 1 )

Filter hooks to allow modify various types of internal data at runtime.

Parameters

  • $tag (string) (Required) The name of the filter to add the callback to.

  • $callback (callable) (Required) The callback to be run when the filter is applied.

  • $priority (int) (Optional) Used to specify the order in which the functions associated with a particular filter 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 filter. Default value: 10

  • $accepted_args (int) (Optional) The number of arguments the function accepts. Default value: 1

How to pass callback function?

The callback function could be a string referring to a class in the application MyNamespace\Http\Listener@myHookListener, an array callback [$object, 'method'] or a globally registered function global_function, anonymous function.

Example using anonymous function:

add_action('user_created', function($user) {
    $user->sendWelcomeMail();
}, 20, 1);

Example using referring to a class's method:

add_action('user_created', 'MyNamespace\Http\MyClass@myMethod', 20, 1);

Example using array callback:

add_action('user_created', [$object, 'myMethod'], 20, 1);

Usage

Actions

Wherever you want, you can create a new action in you Laravel application:

do_action('user_created', $user);

Here, user_created is the name of the action, which will use later when the action will be listening. And $user is the parameters, which will be found whenever you listen the action. These can be anything.

To listen to your actions, you attach listeners. These are best added to your AppServiceProvider boot() method.

For example if you wanted to hook in to the above hook, you could do:

add_action('user_created', function($user) {
    $user->sendWelcomeMail();
}, 20, 1);

The first argument must be the name of the action. The second would be a closures, callbacks and anonymous functions. The third 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 value: 10. And fourth, the number of arguments the function accepts. Default value: 1

Filters

Filters always have to have data coming in and data going out to ensure the data is output in the browser (your content may be passed through other filters before getting output in the browser). By comparison, actions, which are similar to filters, do not require that anything to be returned, although data can be returned through actions as well.

Basically, filters are functions that can be used in Laravel application to pass data through. They allows developers to modify the default behavior of a specific function.

Here's an example of how filter used in a real application.

Post.php is a model or class , where it builds a query to fetch all published posts

class Post extend Model
{
    public function getPublished()
    {
        return Post::where('published_at', '>', now());
    }
}

Using filter we can modify this query:

class Post extend Model
{
    public function getPublished()
    {
        return apply_filters('posts_published', Post::where('published_at', '>', now());
    }
}

Now, in the entry point of application like any module or plugin's you can modify this post published query.

In Module's or Plugin's service provider (preferably in the boot method) we'll add a listener for the filter.

class ModuleServiceProvider extends ServiceProvider
{
    public function boot()
    {
        add_filter('posts_published', function($query) {
            return $query->where('status', 'active');
        });
    }
}

Support us

We invest a lot of resources into video tutorials and creating open-source packages. If you like what I do or if you ever made use of something I built or from my videos, consider supporting us. This will allow us to focus even more time on the tutorials and open-source projects we're working on.

Yanselmask, Inc

Thank you so much for helping us out! 🥰

License

The MIT License (MIT). Please see License File for more information.

yanselmask/laravel-hooks 适用场景与选型建议

yanselmask/laravel-hooks 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 38 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 07 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「wordpress」 「filter」 「HOOK」 「laravel」 「filters」 「hooks」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 yanselmask/laravel-hooks 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 yanselmask/laravel-hooks 我们能提供哪些服务?
定制开发 / 二次开发

基于 yanselmask/laravel-hooks 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 38
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 18
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-07-30