Menu

Requests
Handling Requests with Leaf

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 request methods. The request section basically deals with requests made to the app, so far, there are only two functions chained to the Request class. To use the request object, you simply need to pass $request into your $route like this.

Note that from v1.3, Request is part of the HTTP namespace.

Including Request

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

use Leaf\Core\Http\Request; $request = new Request(); $leaf->post('/name/add', function() use($request) { $name = $request->getParam('name'); });
Here, we set $request to the Request class, and we pass it into the route with use().

Request Methods

getParam()

getParam is a method built for APIs, but can be used in a normal web app/website. It takes in one parameter: the name of the param passed into the app through the request It works just like how $_POST['key'] does

use Leaf\Core\Http\Request; $request = new Request(); $leaf->post('/name/add', function() use($request) { $name = $request->getParam('name'); });

getParam() now supports native get and post requests...as well as put requests

// get: linkToApp?id=1 $id = $request->getparam('id');
Posts from forms and PUT data can all be accessed with getParam

getBody()

getBody() returns the key => value pairs of all the request data

use Leaf\Core\Http\Request; $request = new Request(); $leaf->post('/name/add', function() use($request) { $body = $request->getBody(); });

Next Steps

Simple Routing
Response
Simple Authentication
Database Config