Menu

Response
Leaf's handy response class

Overview

Leaf tries as much as possible to use raw PHP written from scratch, so, instead of using PSR-7 like most frameworks do, Leaf has written predefined response methods.
In v1.3, Request is now part of the HTTP namespace.

Including Response

To include the Response object in a route, use this:

use Leaf\Core\Http\Response; $response = new Response(); $leaf->get('/', function() use($response) { $response->renderPage('./link/to/page'); });
Here, we set $response to the Response class, and we pass it into the route with use().

Response Methods

In v1.3, Response methods no longer use the echo keyword. Everything is handled in Response.

respond()

Respond is a method built for APIs and direct responses to the user. This response is encoded as JSON and output as such

use Leaf\Core\Http\Response; $response = new Response(); $leaf->get('/', function() use($response) { $response->respond(["message" => "something"]); });

respondWithCode()

RespondWithCode is also a method built for APIs and direct responses to the user. This response is encoded as JSON and output as such, but it includes a section for the http code(default is 200)

use Leaf\Core\Http\Response; $response = new Response(); $leaf->get('/', function() use($response) { $response->respondWithCode(["message" => "something"], 200); });

throwErr()

throwErr is also a method built for APIs and direct responses to the user, however, this method is suitable for errors only. The response is encoded as JSON and output as such, also, the correct headers are set for the error. It also includes a section for the http error code.
More info on http status codes here

use Leaf\Core\Http\Response; $response = new Response(); $leaf->get('/name', function() use($response) { $name = "something"; $name == null ? $response->throwErr('Name is null', $code) : null; });

renderPage()

In v1.4.2, renderPagePage has been renamed to renderPage for clarity and simplicity.

renderPage is a method built for websites and webapps. This method is can be used with the routing to achieve server side routing, for example:

use Leaf\Core\Http\Response; $response = new Response(); $leaf->get('/homepage', function() use($response) { $response->renderPage('link/to/home.html'); });
With this, whenever the route /homepage is invoked, Leaf loads up home.html and outputs it to the user

renderMarkup()

renderMarkup is another method built for websites and webapps. This method outputs any html entered into it with a content type of text/html:

For instance, with this code,
$code = "

Hello

";
We simply pass it into the response...like this
use Leaf\Core\Http\Response; $response = new Response(); $leaf->get('/homepage', function() use($response) { // $code defined $response->renderMarkup($code); });
You might be wondering why we don't just use
echo "

hello

";
The reason is, Leaf has default headers which set the content type to JSON, in order to correctly output HTML, you need to change this....Leaf has taken care of this, all within renderMarkup and renderPage

Next Steps

Simple Routing
Request
Simple Authentication
Database Config