Request Lifecycle
Relevant source files
The following files were used as context for generating this wiki page:
The request lifecycle in the QashierWise backend describes the entire journey of an HTTP request, from the moment it is received by the server to the point a response is sent back to the client. As a Laravel-based application, it follows a standard but customizable flow involving an entry point, routing, middleware, controller logic, and response generation. This process ensures that requests are authenticated, validated, and processed securely and efficiently.
Understanding this lifecycle is crucial for developers to debug issues, add new features, and comprehend how different components of the application interact to handle user and API interactions, particularly for core functionalities like WhatsApp Account Management and Template Management.
High-Level Request Flow
All requests to the QashierWise application follow a consistent, high-level path. The diagram below illustrates the major stages a request passes through.
Detailed Breakdown
1. Entry Point: public/index.php
Every request to the application is first handled by the public/index.php file. This script is the universal entry point for all web and API requests. Its primary responsibilities are:
- Loading the Composer-generated autoloader.
- Bootstrapping the Laravel application instance from
bootstrap/app.php. - Passing the incoming request to the Laravel HTTP kernel.
- Sending the generated response back to the client.
This file marks the beginning of the framework's control over the request.
Sources: public/index.php
2. Routing
Once the application is bootstrapped, the HTTP Kernel directs the request to the router. The router's job is to match the request's URI and HTTP method to a specific controller action or closure. API routes are defined in routes/api.php, while web routes are in routes/web.php.
The frontend Blade templates reveal the structure of the API. For example, the templates.blade.php file contains JavaScript logic that interacts with a set of RESTful endpoints for managing WhatsApp templates.
The following table summarizes some of the key API routes inferred from the frontend code:
| Method | URI | Action | Source File |
|---|---|---|---|
GET | /api/whatsapp/templates | Fetches all templates. | resources/views/dashboard/templates.blade.php:1217 |
POST | /api/whatsapp/templates | Creates a new template. | resources/views/dashboard/templates.blade.php:1523 |
PUT | /api/whatsapp/templates/{id} | Updates an existing template. | resources/views/dashboard/templates.blade.php:1865 |
DELETE | /api/whatsapp/templates/{name} | Deletes a template. | resources/views/dashboard/templates.blade.php:1941 |
GET | /api/whatsapp/account | Fetches the connected WhatsApp account status. | resources/views/dashboard/whatsapp-account.blade.php:433 |
DELETE | /api/whatsapp/account | Disconnects the WhatsApp account. | resources/views/dashboard/whatsapp-account.blade.php:596 |
GET | /api/pos/categories | Fetches all POS categories. | resources/views/dashboard/pos/categories.blade.php:188 |
POST | /api/pos/categories | Creates a new POS category. | resources/views/dashboard/pos/categories.blade.php:230 |
Sources: resources/views/dashboard/templates.blade.php, resources/views/dashboard/whatsapp-account.blade.php, resources/views/dashboard/pos/categories.blade.php
3. Middleware
After a route is matched, the request is sent through a pipeline of middleware before it reaches the controller. Middleware can perform tasks like authentication, logging, or modifying the request. For example, the application includes a middleware named EnsureWhatsAppConnected.php. Although its content is not provided, its name strongly implies that it protects certain routes by verifying that a user has an active WhatsApp account connected, redirecting or returning an error if they do not.
This diagram illustrates the middleware flow for a protected API route.
Sources: app/Http/Middleware/EnsureWhatsAppConnected.php (by reference)
4. Controller Layer
The request, having passed through all relevant middleware, finally arrives at the designated controller method. Controllers are responsible for orchestrating the business logic. They receive the request, delegate tasks to service classes, and prepare the response.
All controllers in the application extend the base App\Http\Controllers\Controller class, which itself uses AuthorizesRequests and ValidatesRequests traits to provide common authorization and validation capabilities.
Sources: app/HttpControllers/Controller.php:10-13
5. Service Layer: TemplateService
For complex operations, controllers delegate logic to dedicated service classes. The TemplateService is a prime example, encapsulating all logic related to WhatsApp template management. It is responsible for validation, building API payloads, and communicating with the Meta (Facebook) Graph API.
Input Validation
Before any action is taken, TemplateService performs rigorous validation. The validation rules are strictly defined in the service and confirmed by property-based tests.
| Field | Rule | Source File |
|---|---|---|
| Template Name | Required. Must contain only lowercase letters (a-z), numbers (0-9), and underscores (_). | app/Services/TemplateService.php:32-46 |
| Body | Required. Maximum 1024 characters. | app/Services/TemplateService.php:73-87 |
| Footer | Optional. Maximum 60 characters. | app/Services/TemplateService.php:54-65 |
| Buttons | Optional. Cannot mix button types. Max 10 QUICK_REPLY buttons OR max 2 CTA (URL/PHONE_NUMBER) buttons. | app/Services/TemplateService.php:95-128 |
| Required Fields | name, category, language, and body are all mandatory for creation. | app/Services/TemplateService.php:136-157 |
| Variable Sequence | A warning is generated if variables like {{1}}, {{2}} are not sequential. | app/Services/TemplateService.php:165-186 |
Sources: app/Services/TemplateService.php, tests/Unit/Services/TemplateServicePropertyTest.php
Business Logic & API Interaction
The service contains methods that map directly to the template management lifecycle:
createTemplate(array $data): Validates data, builds a component payload usingbuildComponents(), and sends aPOSTrequest to/{waba-id}/message_templates.updateTemplate(string $templateId, array $data): Validates and builds an updated component payload, then sends aPOSTrequest to/{template-id}.deleteTemplate(string $templateName, ?string $templateId): Sends aDELETErequest to/{waba-id}/message_templateswith the template name as a query parameter.
Sources: app/Services/TemplateService.php:269-450
Data Serialization
To store template structures in the database, the service uses serialization and deserialization methods. The components and buttons arrays, which can be complex nested structures, are converted to JSON strings for storage and decoded back into arrays when retrieved.
serializeTemplate(array $data): Encodescomponentsandbuttonsarrays into JSON strings.deserializeTemplate(array $data): DecodescomponentsandbuttonsJSON strings back into arrays.
Property-based tests confirm that this round-trip process is lossless and preserves the original data structure perfectly.
Sources: app/Services/TemplateService.php:457-493, tests/Unit/Services/TemplateServicePropertyTest.php:455-481
6. Response Generation
After the controller receives a result from the service layer, it generates an HTTP response.
- For API requests, this is typically a JSON response containing a
successstatus, adatapayload, and anerrormessage if applicable. The frontend JavaScript in files liketemplates.blade.phpis built to handle this structure. - For web requests, the controller would return a rendered Blade view, such as
welcome.blade.phpordashboard/index.blade.php, passing data to it.
Sources: resources/views/dashboard/templates.blade.php:1546-1555, resources/views/welcome.blade.php
Sequence Diagram: Creating a Template
This diagram shows the detailed sequence of events for a POST /api/whatsapp/templates request, illustrating the full lifecycle from client to external API and back.
Conclusion
The request lifecycle in QashierWise is a structured and robust process built upon the Laravel framework. It effectively separates concerns, with routing, middleware, controllers, and services each playing a distinct role. The TemplateService is a key component that demonstrates how business logic, validation, and external API communication are encapsulated. This architecture allows for maintainable and scalable development, ensuring that all incoming requests are handled in a predictable and secure manner.