Overview
Leaf v1.4.2 includes a core api controller which you can extend to create your own controllers. This gives you access to a bunch of handy functions for use in your API.
Including the API controller
To include the API Controller object in a route, use this:
use Leaf\Core\ApiController;
class NameController extends ApiController
After this, you should have access to Leaf's core API controller methods
Controller Methods
Responses
Leaf Core controller contains methods to appropriately return data to the user.respond
respond returns json encoded data to the user.
use Leaf\Core\ApiController;
class NameController extends ApiController {
public function index() {
$this->respond([
"message" => "hello"
]);
}
}
respondWithCode
respond returns json encoded data to the user along with an HTTP code.
use Leaf\Core\ApiController;
class NameController extends ApiController {
public function index() {
$this->respondWithCode([
"message" => "hello"
], 200);
}
}
file_upload
file_upload is for simple file uploads. It takes in 3 parameters, the path to save the file, the file and the file type(optional).
use Leaf\Core\ApiController;
class NameController extends ApiController {
public function index() {
$profilePic = $_FILES["profile_pic"];
$this->file_upload("./images/", $profilePic);
$this->file_upload("./images/", $profilePic, "image");
}
}
objectify
Objectify converts an array to an object.
use Leaf\Core\ApiController;
class NameController extends ApiController {
public function index() {
$obj = $this->objectify([
"name" => "Mychi"
]);
}
}
Next Steps
Simple RoutingResponse
Simple Authentication
Database Config