{"id":20660632,"url":"https://github.com/razu91/laravel-hooks","last_synced_at":"2025-04-19T15:11:00.379Z","repository":{"id":261541736,"uuid":"881019647","full_name":"razu91/laravel-hooks","owner":"razu91","description":"The filter and action system in Laravel is inspired by WordPress hooks","archived":false,"fork":false,"pushed_at":"2024-11-12T16:00:44.000Z","size":66,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-29T09:11:47.493Z","etag":null,"topics":["action","event-driven","filter","hooks","laravel","laravel-hooks","publisher-subscriber","wordpress"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/razu91.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-30T19:22:22.000Z","updated_at":"2024-12-08T18:57:58.000Z","dependencies_parsed_at":"2024-11-07T05:37:17.431Z","dependency_job_id":null,"html_url":"https://github.com/razu91/laravel-hooks","commit_stats":null,"previous_names":["razu91/laravel-hooks"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/razu91%2Flaravel-hooks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/razu91%2Flaravel-hooks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/razu91%2Flaravel-hooks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/razu91%2Flaravel-hooks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/razu91","download_url":"https://codeload.github.com/razu91/laravel-hooks/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249723177,"owners_count":21315962,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["action","event-driven","filter","hooks","laravel","laravel-hooks","publisher-subscriber","wordpress"],"created_at":"2024-11-16T19:05:37.058Z","updated_at":"2025-04-19T15:11:00.341Z","avatar_url":"https://github.com/razu91.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel Hooks\n\nThe filter and action system in Laravel is inspired by WordPress hooks\n\n## About\n\n\u003cb\u003eActions\u003c/b\u003e are code snippets that run at specific points in your program. They don't return any value but provide a way to hook into your existing code without causing interference.\n\n\u003cb\u003eFilters\u003c/b\u003e are designed to alter data. They always return a value, typically the modified version of the first parameter, which is the default return behavior.\n\n\u003cb\u003eNotes:\u003c/b\u003e This works similarly to the `Observer design pattern`, or the `Publisher/Subscriber pattern`, which is also related to `event-driven architecture`.\n\n## More Resources\n\n[Read more about filters](http://www.wpbeginner.com/glossary/filter/)\n\n[Read more about actions](http://www.wpbeginner.com/glossary/action/)\n\n[Read more about filters and actions](https://developer.wordpress.org/plugins/hooks/)\n\n\n## Installation\n\n1. Install using Composer\n\n```bash\ncomposer require razu/laravel-hooks\n```\n**Great!** Now you're ready to use laravel-hooks.\n\n## ACTION\n\n#### EVENT\n\n```php\ndo_action( string $tag, mixed $arg )\n```\n\nThe *\u003cb\u003edo_action\u003c/b\u003e* function is used to trigger all callback functions attached to a specified action hook *\u003cb\u003e($tag)\u003c/b\u003e*.\n\n**Parameters**\n- `$tag`\n*(string) (Required) – The name of the action hook that will be triggered. This specifies the point in the code where the action occurs.*\n\n- `$arg`\n*(mixed) (Optional) – Additional arguments that can be passed to the functions hooked to this action. Defaults to empty if not provided.*\n\n\u003csmall\u003e\n    By calling \u003cb\u003e\u003ci\u003edo_action\u003c/i\u003e\u003c/b\u003e, you execute all functions linked to the specified hook \u003cb\u003e\u003ci\u003e($tag)\u003c/i\u003e\u003c/b\u003e, passing any optional arguments to each function. This allows custom code to be run at specific points within the application.\n\u003c/small\u003e\n\n#### LISTENER\n\n```php\nadd_action( string $tag, callable $callback, int $priority = 10, int $accepted_args = 1 )\n```\n\nThe *\u003cb\u003eadd_action\u003c/b\u003e* function associates a callback function with a specified hook *\u003cb\u003e($tag)\u003c/b\u003e* in a Laravel or WordPress-style hook system. Here’s what each parameter does:\n\n**Parameters**\n- `$tag`\n*(string) (Required) The name of the hook to which the callback function should be attached.*\n\n- `$callback`\n*(callable) (Required) The function to be executed when the hook is triggered.*\n\n- `$priority`\n*(int) (Optional) Determines the order in which the functions associated with a particular hook are executed. Lower numbers correspond to earlier execution. Default value: 10*\n\n- `$accepted_args`\n*(int) (Optional) The number of arguments the callback function accepts. Default value: 1*\n\nBy using *\u003cb\u003eadd_action\u003c/b\u003e*, you can ensure that your custom functions are executed at the appropriate points in your application’s lifecycle, \nallowing you to extend or modify functionality as needed.\n\n## FILTER\n\n#### EVENT\n```php\napply_filters( string $tag, mixed $value )\n```\n\nThe *\u003cb\u003eapply_filters\u003c/b\u003e* function calls all functions attached to a specific filter hook *\u003cb\u003e($tag)\u003c/b\u003e*. This allows you to modify a value by passing it through multiple filters. \n\n**Parameters**\n- `$tag`\n*(string) (Required) – The name of the filter hook to trigger. This identifies the specific filter to apply.*\n- `$value`\n*(mixed) (Required) – The value to be filtered. Each function attached to the hook can modify and return this value.*\n\nWhen *\u003cb\u003eapply_filters\u003c/b\u003e* is called, it sequentially runs all functions associated with hook *\u003cb\u003e($tag)\u003c/b\u003e*, passing *\u003cb\u003e$value\u003c/b\u003e* through each one. This allows custom modifications to be made to the value at specific points in the application.\n\n#### LISTENER\n\n```php\nadd_filter( string $tag, callable $callback, int $priority = 10, int $accepted_args = 1 )\n```\n\nThe *\u003cb\u003eadd_filter\u003c/b\u003e* function allows you to modify various types of internal data at runtime by attaching callback functions to specific filter hooks.\n\n**Parameters**\n\n-`$tag`\n*(string) (Required) – The name of the filter to which the callback will be added. This specifies the specific filter hook where data modification should occur.*\n\n-`$callback`\n*(callable) (Required) – The callback function to be executed when the filter is applied. This function should process and potentially modify the filtered data.*\n\n-`$priority`\n*(int) (Optional) – Specifies the order in which functions associated with this filter will execute. Lower numbers correspond to earlier execution. Functions with the same priority are executed in the order they were added. The default is 10.*\n\n- `$accepted_args`\n*(int) (Optional) – The number of arguments the callback function accepts. The default is 1.*\n\nUsing *\u003cb\u003eadd_filter\u003c/b\u003e*, you can ensure that specific functions modify data when the designated filter is applied, allowing for flexible data processing throughout the application.\n\n## Here’s how to pass a callback function:\n\nThe callback can be specified in various ways, including:\n- An anonymous function.\n- A string referring to a class and method within the application, like `OurNamespace\\Http\\Listener\\ourClass@ourHookListenerMethod`.\n- An array format, such as `[$object, 'method']`.\n- A globally defined function, such as `global_function`.\n\n\n#### Examples\n\n- Using an anonymous function:\n```php\nadd_action('user_registered', function($user) {\n    $user-\u003esendEmailVerificationCode();\n}, 20, 1);\n```\n\n- Using a class method reference as a string:\n```php\nadd_action('user_registered', 'OurNamespace\\Http\\Listener\\ourClass@ourHookListenerMethod', 20, 1);\nadd_action('user_registered', 'OurNamespace\\Http\\Controllers\\ourController@ourMethodName', 20, 1);\n```\n\n- Using an array callback:\n```php\nadd_action('user_registered', [$object, 'ourMethod'], 20, 1);\n```\n\n\n## Usages\n### Actions\nAnywhere in your code, you can define a new action like this:\n\n```php\ndo_action('user_registered', $user);\n```\n\nHere, `user_registered` is the action name, which will be used later when the action is being listened to. \nThe `$user` parameter will be available whenever you listen for the action. This parameter can represent any relevant data.\n\nTo listen for your actions, attach listeners at the appropriate points. Another good approach is to add these in the `boot()` method of your `AppServiceProvider`.\n\nFor example, if you want to hook into the above action, you could do the following:\n\n```php\nadd_action('user_registered', function($user) {\n    $user-\u003esendEmailVerificationCode();\n}, 10, 1);\n```\n\nThe first argument must be the name of the action. The second argument can be closures, callbacks, or anonymous functions. \nThe third argument specifies the order in which the functions associated with a particular action are executed. \nLower numbers correspond to earlier execution, and functions with the same priority are executed in the order in which they were added to the action. \nThe default value is 10. The fourth argument specifies the number of arguments the function accepts, with the default value being 1.\n\n\n### Filters\n\nFilters must always have data coming in and going out to ensure it reaches the browser for output.\nYour content might pass through multiple filters before finally being displayed. \nIn contrast, actions—though similar to filters—do not require any data to be returned. However, \ndata can still be passed through actions if needed.\n\nFilters are functions in Laravel applications that allow data to be passed through and modified.\nThey enable developers to adjust the default behavior of specific functions.\n\nHere’s an example of how a filter can be used in a application.\n\nIn the `Product.php` model, we have a `getAvailable` method that builds a query to fetch all available products:\n\n```php\nclass Product extends Model\n{\n    public function getAvailable()\n    {\n        return Product::where('available_at', '\u003c=', now());\n    }\n}\n```\n\nUsing a filter, we can modify this query dynamically:\n\n```php\nclass Product extends Model\n{\n    public function getAvailable()\n    {\n        return apply_filters('products_available', Product::where('available_at', '\u003c=', now()));\n    }\n}\n```\n\nNow, in the entry point of the application, such as in a module or plugin, you can modify this product availability query.\n\nIn the service provider of the module or plugin (ideally within the boot method), we'll register a listener for the filter.\n\n```php\nclass ModuleServiceProvider extends ServiceProvider\n{\n    public function boot()\n    {\n        // Registering a filter to modify the query for available products\n        add_filter('products_available', function($query) {\n            return $query-\u003ewhere('status', 'active');\n        });\n    }\n}\n```\n\n## License\n\nThis software is licensed under the \u003ca href=\"https://github.com/razu91/laravel-hooks/blob/main/LICENSE\"\u003e(MIT)\u003c/a\u003e License. For more details, please refer to the \u003ca href=\"https://github.com/razu91/laravel-hooks/blob/main/LICENSE\"\u003eLicense\u003c/a\u003e file.\n\n\n### Related Link\n\n\u003ca href=\"https://packagist.org/packages/razu/laravel-hooks\"\u003ehttps://packagist.org/packages/razu/laravel-hooks\u003c/a\u003e\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frazu91%2Flaravel-hooks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frazu91%2Flaravel-hooks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frazu91%2Flaravel-hooks/lists"}