Skip to content


This is a simple object which allows you to manage the way headers are used in your application. It contains methods to set and get headers in your app.


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.


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.

// ...
echo 'Page not found';

You can also return the currently set status code.

$code = Leaf\Http\Headers::status();


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.

// ...
echo 'Something here';


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

// will not sanitize headers

// will sanitize headers


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 allows you to add 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.

  'WWW-Authenticate' => 'Negotiate',
  'WWW-Authenticate' => 'NTLM'
], null, false);


This method removes previously set headers.

// single value

// multiple value
Leaf\Http\Headers::remove(['Content-Type', 'WWW-Authenticate']);


This method allows you to check if a header has been set in the current request. It returns true if the header has been set and false otherwise.

if (Leaf\Http\Headers::has('X-SOME-HEADER')) {
  // do something nice

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.


This method set's the content type of the response to text/plain, it also takes in an HTTP status code.

echo 'plain text here';


This method set's the content type of the response to text/html, it also takes in an HTTP status code.

echo 'html here';


This method set's the content type of the response to application/xml, it also takes in an HTTP status code.

echo 'Xml here';


This method set's the content type of the response to application/json, it also takes in an HTTP status code.

echo 'json here';


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', '', 200);

You can set mutiple access control headers at once:

Leaf\Http\Headers::accessControl(['Allow-Origin' => '*', 'Allow-Headers' => '*']);
Headers has loaded