Request Headers
In Phlame's API development toolkit, the headers method within the API class provides a convenient way to handle HTTP headers in incoming requests. This method offers flexibility and ease of use, allowing developers to check for specific headers, retrieve all headers, and execute callbacks based on the results. Let's dive into the syntax and usage of the headers method:
Checking for Headers
The headers method enables developers to check for the presence of headers in incoming requests. Here's how you can use it:
Api::headers():
Checks if any headers are present in the incoming request and returns status: true if headers are found, along with data containing all headers.
Api::headers();
Api::headers(key: "key"):
Checks if a header with the specified key is present in the incoming request. If found, it returns status: true along with data containing all headers.
Api::headers(key: "key");
Api::headers(key: "key", value: "value"):
Checks if a header with the specified key has a value equal to the provided value. If found, it returns status: true along with data containing all headers.
Api::headers(key: "key", value:"value");
Handling Results with Callbacks
The headers method also supports additional parameters then: and orelse:, which are callbacks executed based on the status of the header check:
Then Callback:
This callback runs when the header check returns status: true. It allows developers to execute custom logic based on the presence or values of headers.
Orelse Callback:
This callback runs when the header check returns status: false. It provides a fallback mechanism for scenarios where the expected headers are not present in the incoming request.
You can use the variable $rout
inside the callback functions to get the data the headers
method will return, that includes
- Status
- Data
Example of a Header Method
<?php
Api::headers(
key: "Test-Header",
value: "Test Value",
then: function() {
// Code to execute
},
orelse: function() {
// Code to execute
}
);
?>