Hooks and Handlers
Hooks and Handlers Hooks and handlers allow you to customize API behavior at different points in the request lifecycle. Hooks run before and after handlers, while handlers contain the main business logic. Quick Navigation Overview - What are hooks and handlers preHooks - Execute
Hooks and Handlers
Hooks and handlers allow you to customize API behavior at different points in the request lifecycle. Hooks run before and after handlers, while handlers contain the main business logic.
Quick Navigation
- Overview - What are hooks and handlers
- preHooks - Execute before handler
- postHooks - Execute after handler
- Custom Handlers - Replace default CRUD
- Common Patterns - Real-world examples and best practices
Overview
Hooks
Hooks are code snippets that run at specific points in the request lifecycle: - preHooks: Execute before the handler - postHooks: Execute after the handler
Handlers
Handlers contain the main business logic: - Custom Handler: Your custom code - Default CRUD: Automatic CRUD operation based on HTTP method
Execution Flow
Pre-Auth Guards Auth Post-Auth Guards preHook #1 preHook #2 Handler postHook #1 postHook #2
Guards run before hooks and handle IP blocking / rate limiting. Hooks run after guards and handle validation / business logic.
All hooks and handlers have access to the same $ctx object, so changes in one phase are visible to all subsequent phases.
Documentation
- preHooks - Validation, data transformation, and permission checks
- postHooks - Response transformation, audit logging, and side effects
- Custom Handlers - Custom business logic and hook types
- Common Patterns - Best practices and real-world examples
Next Steps
- Learn about Repository Methods for database operations
- See Context Reference for all available properties
- Check API Lifecycle to understand execution order