Request and response
Request and response are the basis of any interaction on the web. When we visit a web page with our device (client), we send a request to the destination server. The destination server accepts the request and verifies its validity. Based on the type of request, it executes the instructions and returns a response to the client. The client interprets the response and displays it to the user.
Request
A request is the set of information that a client (usually a browser) sends to a server when it wants to obtain a resource or perform an action. A request typically contains:
- HTTP method (GET, POST, PUT, DELETE, etc.)
- Requested URL and any query parameters (e.g., ?page=2)
- Headers (User-Agent, Accept, Authorization, etc.)
- Body (data sent)
- Cookie
Request lifecycle
- The client sends a request to the server
- The server checks if there is a route that matches the requested address
- If the route has middlewares, the request passes through each middleware before reaching the final handler
- The server interprets the request, executing the necessary logic (database, services, etc.), and generates the response
- The response passes back through the middlewares again and is sent to the client
Get the request instance
You can get the request instance and access its values in 3 ways:
Through the \App\System\Http\ServerRequest class
By calling request() helper
By type hinting a view argument as \App\System\Http\ServerRequest
Once you've obtained the request instance, you can access the values using the all and input methods.
all returns an array of all values, while input takes the name of the value's key as its first argument, and optionally, a fallback value as its second argument if the key doesn't exist.
$all_values = $request->all();
$some_value = $request->input("some_value");
$some_undefined_value = $request->input("some_undefined_value", "fallback_value");
Use filled to check if a key is present in the request.
The add method allows you to add a value to the request.
Like the add method, the addFile method allows you to add a file to the request.
$request = $request->..., as anticipated, this is due to the fact that the request object is immutable.The isAjax method returns true if the request was made via Ajax. For recognition to work correctly, the request must contain the X-Requested-With header and its value must be XMLHttpRequest.
Response
A response is what the server sends back to the client after processing the request. A response typically contains:
- Status code (200, 404, 500, etc.)
- Header (Content-Type, Cache-Control, etc.)
- Body (returned data)
- Cookie to be set
Allowed response types
Whenever a request is handled by a route, the route handler can return a value that will be sent as a response to the client. The return values can be of various types. Let's see what they are and some examples.
String
\App\System\Http\RedirectResponse
new \App\System\Http\RedirectResponse() takes 3 arguments:
- location (required): the destination URL
- headers (optional): the response headers
- response code (optional): the HTTP response code, default 302
The redirect() helper is a convenience shortcut built on top of it, but takes 4 arguments in a different order — the second is session flash data, not headers:
- location (required): the destination URL
- data (optional): an array with
with_inputs(bool, re-flashes the current request's input) and/ormessages(array, flashed to the session) - response code (optional): the HTTP response code, default 302
- headers (optional): the response headers
\App\System\MVC\View\View
return view("my_view");
//or
return \App\System\MVC\View\View::get("view_name", [** optional arguments**]);
//or
return module_view("ModuleNamespace::view_name");
//or
return \App\System\MVC\View\View::module("ModuleNamespace::view_name", [** optional arguments**]);
For further details read the views documentation.
\App\System\MVC\Model
Models will be converted to json.
stdClass
stdClass objects will be converted to json.
bool
array
Array will be converted to json.
\Illuminate\Support\Collection
\Illuminate\Support\Collection will be converted to json.
Implemented PSR standards
Request and Response follow PSR standards to ensure interoperability and consistency between installed packages and features. They are built on top of laminas/laminas-diactoros, which provides the base PSR-7/PSR-17 implementation — Monti extends its classes to add framework-specific conveniences (like the ones documented above).
The implemented standards are:
| PSR | Description |
|---|---|
| PSR‑7: HTTP Message Interface | Defines how the objects representing requests and responses should be structured. The objects are immutable, each change creates a new object |
| PSR‑15: HTTP Server Request Handlers and Middleware | Defines how a framework should manage the request flow |
| PSR‑17: HTTP Factories | Defines the standards for creating the HTTP objects used in the PSR‑7 standard |