Concept and Architecture
Understanding the request lifecycle in BowPHPβ
BowPHP is a lightweight PHP framework designed to provide a clear, modular structure. When a request arrives, it follows a well-defined path called the request lifecycle. This cycle is organized to separate responsibilities, make the code easy to maintain, and improve performance.
The request lifecycle explainedβ
Every request goes through several steps before generating a response for the user.
Here are the main steps:
- HTTP request: The user sends a request through their browser or an API.
- Request: The framework captures this request and extracts the necessary data (URL, parameters, body, etc.).
- Kernel: The Kernel is the heart of the framework. It:
- Loads the configuration files located in the
configfolder (such asconfig/database.php). - Initializes the modules needed by the application.
- Application: It starts the Kernel and prepares the environment to handle the request.
- Router:
- The router detects the route matching the request (for example,
/users/42or/api/login). - It associates this route with a specific controller and method, defined directly in the route code.
- If middlewares are defined for this route, they are executed before the controller is called.
- Middleware: Middlewares perform specific processing, such as checking authentication or validating data.
- Controller: Once the route is validated, the controller executes the business logic (for example, retrieving data or performing an action).
- Model: If data is needed, the model interacts with the database to retrieve or update it.
- View: The controller passes the retrieved data to the View component, which generates the final content (HTML, JSON, etc.).
- HTTP response: The response is sent to the user.
The router's role in detailβ
The router is a key component of BowPHP. It acts as a "compass" that directs each request to the right controller and the right method.
Route detectionβ
The router examines the request URL and determines which route matches. Routes are defined in dedicated files (often routes/web.php or routes/api.php).
Example of a route definition:
$app->get('/users/{id}', 'UserController@show');
$app->post('/api/login', 'AuthController@login');
Here:
The GET /users/42 request is associated with the show method of the UserController controller.
The POST /api/login request is associated with the login method of the AuthController controller.
Association with the controllerβ
Once the route is detected, the router:
- Validates the URL parameters (such as
{id}). - Executes the middlewares defined for this route (if any).
- Calls the controller and method associated with the route.
A modular architectureβ
BowPHP is built on a modular architecture. Each module is independent, has its own configuration, and fulfills a specific role. This makes the application easy to manage and extend. The router is one example of this modular approach. Another example is the Database module, which manages database connections.
Example: The Database moduleβ
Let's take the case of the Database module, which manages database connections:
Configuration file: config/database.php
This file contains the parameters needed to connect to the database. Here is a simple example:
return [
'default' => 'mysql',
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => 3306,
'database' => 'bowphp_app',
'username' => 'root',
'password' => '',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
],
],
];
The Bow\Database\DatabaseConfiguration classβ
This class is responsible for reading the parameters from the config/database.php file and establishing a connection to the database.
The Kernel's roleβ
At startup, the Kernel loads the config/database.php file using DatabaseConfiguration defined in app/Kernel.php and initializes a database connection, ready to be used by other components.
Visualization with a diagramβ
Here is a visual representation to better understand the request lifecycle and modular integration (example with Database): Loads the configurations
Here is an updated diagram highlighting the role of the router and the modular integration (example with Database)
Summary of the key pointsβ
- Structured lifecycle: A request follows a precise path (Request β Kernel β Router β Middleware β Controller β Model β View β Response).
- The router's role: The router detects the route matching the request and calls the associated controller and method.
- Kernel: Orchestrates the loading of configurations from the config folder and initializes the modules.
- Modularity: Each component (router, database, etc.) is independent, which simplifies management and improves maintainability.
Contributingβ
Thank you for considering contributing to BowPHP! The contribution guide can be found in the documentation.
Is something missing?
If you run into problems with the documentation or have suggestions to improve the documentation or the project in general, please open an issue for us, or send a tweet mentioning the Twitter account @bowframework or directly on github.