Request Parameters
In Phlame's API development toolkit, the params
method within the API class provides a straightforward approach to handle HTTP request bodies in incoming requests. This method offers developers flexibility and simplicity, enabling them to process request payloads efficiently and execute custom logic based on the contents. Let's explore the syntax and usage of the params
method:
Retrieving Request Params
The params
method allows developers to access the content of the HTTP request params. Here's how you can use it:
Api::params()
:
This method retrieves the entire request parameters and returns it as a string, with a status, true, if the parameters exists.
Api::params();
Other functions of the params
method functions similar to the headers
method, it takes the same parameters as the headers
method, check the headers method out for more information
Handling Results with Callbacks
Similar to the headers
method, the params
method also supports then:
and orelse:
callbacks, allowing developers to execute custom logic based on the contents of the request params:
Then Callback:
This callback runs when the parameters retrieval is successful, and the status
is true
, providing developers with the request parameters content. It allows for processing and manipulation of the request data.
Orelse Callback:
In case the parameters retrieval fails, and the status
is false
, the orelse:
callback is executed. This provides developers with a fallback mechanism to handle such scenarios gracefully.
You can utilize the $rout
variable within the callback functions to access the data returned by the params
method, which includes:
- Status
- Data
Example of the params Method
<?php
Api::params(
key: "key",
value: "value",
then: function($rout) {
// Custom logic to process the request params
$params = $rout['data']; // Accessing the request params content
},
orelse: function() {
// Fallback logic for handling empty or malformed request bodies
}
);
?>
This example illustrates how to use the params
method to retrieve and process the content of the HTTP request params. Depending on the success or failure of the params retrieval, the appropriate callback is executed, allowing developers to handle request params processing effectively.