Request Lifecycle
Understanding the request lifecycle is one of the most important concepts in Bejibun. Every HTTP request follows a predictable path through the framework before a response is returned to the client. By understanding this flow, you can:- Debug applications more effectively
- Know where code should be placed
- Build maintainable architectures
- Understand framework internals
- Optimize application performance
High-Level Overview
Every incoming request follows a sequence of steps:Lifecycle Overview
The complete lifecycle can be visualized as:1. Request Arrival
The lifecycle begins when a client sends an HTTP request. Example:- Web browsers
- Mobile applications
- Frontend frameworks
- External APIs
- Automated services
2. Application Bootstrap
Before handling requests, Bejibun initializes the application. During bootstrap, the framework loads:3. Route Resolution
After initialization, the router determines which route matches the request. Example route:4. Middleware Execution
Before reaching a controller, the request passes through middleware. Middleware acts as a filtering layer. Example:- Authentication
- Authorization
- Rate Limiting
- Logging
- Security Checks
- Request Transformation
Example Authentication Middleware
5. Request Validation
After middleware completes, incoming data may be validated. Example request:6. Controller Execution
If validation succeeds, the controller action executes. Example:- Handling requests
- Coordinating services
- Returning responses
7. Business Logic Layer
Complex operations should be delegated to services. Instead of:- Reusability
- Maintainability
- Testability
- Separation of concerns
8. Database Operations
Most applications interact with a database. Example:9. Response Generation
After processing is complete, a response is created. Example:- JSON
- Text
- Streams
- Files
- Redirects
Explicit Responses
Example:10. Response Sent
Finally, the response is returned to the client.Error Handling
Errors can occur at any stage. Examples:Middleware Pipeline
Middleware execution follows a pipeline pattern. Example:Complete Example
Consider the following endpoint:Lifecycle Best Practices
When building applications:Keep Controllers Thin
Good:Validate Early
Validate input before business logic executes.Use Middleware Appropriately
Middleware should handle cross-cutting concerns such as:- Authentication
- Logging
- Security
Centralize Business Logic
Place business rules in services rather than controllers.Handle Errors Consistently
Use framework-provided error handling whenever possible.Visual Summary
What’s Next?
You now understand how requests flow through a Bejibun application and how the framework’s components work together. The next section dives deeper into individual framework features such as:- Routing
- Controllers
- Middleware
- Validation
- Database
- Authentication
- Caching
- Queues
