Leaf Headers
Watch out
Leaf request is a class available on the leaf http module. Check out the http module docs for installation instructions.
In previous versions of Leaf, Headers have been added to the request and response objects and could not be fully accesed directly, however, v2.4 provides a Headers object which allows you perform all header operations smoothly. Another amazing thing is that all Leaf Header methods are static, and so can be called directly without initializing the Headers object.
You can still use most header methods from within the response and request objects, you can refer to those if you want to, however, this package comes with ore features and better useability.
To get started with the Headers object, you simply need to call whatever method you need on the Leaf\Http\Headers
object. Since it's static, there's no need to initialize it.
Headers Methods
Below are the methods you can use on the Headers object:
status
This method sets or returns the base HTTP status of a response. Response methods allow you to directly set http status codes, however, if you want to use PHP's native output methods, you can set the status code here.
// ...
Leaf\Http\Headers::status(404);
echo "Page not found";
You can also return the currently set status code.
$code = Leaf\Http\Headers::status();
resetStatus
If for some reason, you're not able to set the status using status
, you can always fallback to resetStatus
. This method uses PHP's inbuilt http_response_code
.
// ...
Leaf\Http\Headers::resetStatus(200);
echo "Something here";
all
This method returns all headers passed into your Leaf app. It takes in a single optional parameter, whether to sanitize header data or not, it is set to false by default.
// will not sanitize headers
Leaf\Http\Headers::all();
// will not sanitize headers
Leaf\Http\Headers::all(false);
// will sanitize headers
Leaf\Http\Headers::all(true);
get
This method as the name implies returns a particular header.
$content = Leaf\Http\Headers::get("Content-Type");
You can also get multiple headers at the same time.
$headerGroup = Leaf\Http\Headers::get(["Content-Type", "Authorization"]);
Just like all
, you can also sanitize the information from get
.
$data = Leaf\Http\Headers::get("header", true);
set
set
allows you to set a new response header. It takes in 4 parameters:
- The header to to set
- Value for header
- Replace similar header?
- An http status code
Leaf\Http\Headers::set("location", "/home", true, 302);
You can also set multiple values at once.
Leaf\Http\Headers::set(["location" => "/home", "something" => "here"]);
If you want multiple headers with the same name, you can set replace to false
. This will force multiple headers of the same type.
Leaf\Http\Headers::set([
"WWW-Authenticate" => "Negotiate",
"WWW-Authenticate" => "NTLM"
], null, false);
remove
This method removes previously set headers.
// single value
Leaf\Http\Headers::remove("WWW-Authenticate");
// multiple value
Leaf\Http\Headers::remove(["Content-Type", "WWW-Authenticate"]);
Utility Header methods
Some shortcut methods have been prepared for the most used headers, so you won't need to stress yourself writing a bunch of stuff for simple tasks.
contentPlain
This method set's the content type of the response to text/plain
, it also takes in an HTTP status code.
Leaf\Http\Headers::contentPlain(200);
echo "plain text here";
contentHtml
This method set's the content type of the response to text/html
, it also takes in an HTTP status code.
Leaf\Http\Headers::contentHtml(200);
echo "html here";
contentXml
This method set's the content type of the response to application/xml
, it also takes in an HTTP status code.
Leaf\Http\Headers::contentXml(200);
echo "Xml here";
contentJSON
This method set's the content type of the response to application/json
, it also takes in an HTTP status code.
Leaf\Http\Headers::contentJSON(200);
echo "json here";
accessControl
This method allows you to quickly set Access-Control
headers in your app. It takes in 3 parameters:
- The header to set
- The value to set
- A status code (optional)
Leaf\Http\Headers::accessControl("Allow-Origin", "https://example.com", 200);
You can set mutiple access control headers at once:
Leaf\Http\Headers::accessControl(["Allow-Origin" => "*", "Allow-Headers" => "*"]);