Download the PHP package claudye/laravel-hooks without Composer
On this page you can find all versions of the php package claudye/laravel-hooks. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download claudye/laravel-hooks
More information about claudye/laravel-hooks
Files in claudye/laravel-hooks
Package laravel-hooks
Short Description A Laravel Trait for Hooking Before and After Method Calls
License
Informations about the package laravel-hooks
Laravel Hooks
Laravel Hooks
is a Laravel trait that allows you to register hooks (callbacks) before and after calling specified methods in a Laravel controllers. This helps you modify or extend the behavior of methods without altering their core logic.
1. Features and Usage
Key Features:
- Before and After Hooks: Easily register callbacks that are executed before or after specific controller (action) methods.
- Non-intrusive Method Modification: Extend or modify method behaviors without touching the core code.
- Automatic Hook Setup: Use hooks automatically by leveraging a predefined
useHooks
method.
Use Cases:
- Logging: Automatically log actions before or after certain methods are executed.
- Data Validation: Add complex validation logic before the execution of critical methods.
- Response Transformation: Modify or format a method’s return value after it’s called.
- Controller Instance Modification: Modify the controller instance before a method is executed, useful when combining with dependency injection.
- Cross-cutting Concerns: Apply logic across multiple methods like auditing or authorization without duplicating code.
2. Installation
You can install this package via Composer. Make sure your Laravel version is 7.x or higher (up to 11.x supported).
Run the following command to install the package:
3. Example of Usage
Here's an example of how to use the HasControllerHooks
trait in your Laravel controller or class:
Advanced Usage
1. Modifying Controller Instance Before Method Execution
You can modify the controller instance itself before a method is executed, which is particularly useful when working with dependency injection.
Example:
Error Handling
If a method does not exist when trying to call it via the callAction
method, a BadMethodCallException
will be thrown, providing more clarity during development.
4. Requirements
- Laravel 7.x to 11.x
5. License
This package is open-source software licensed under the MIT License.