Menu

Core Controller
Core Controller for your Leaf app

Overview

Leaf v1.4.2 includes a core controller which you can extend to create your own controllers. This gives you access to a bunch of handy functions for use in your app.

Including the Controller

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

use Leaf\Core\Controller; class NameController extends Controller
After this, you should have access to Leaf's core controller methods. Note that Leaf's core controller is already integrated with Leaf Veins and a customised response object, so all Vein and Response methods will be available to you.

Controller Methods

Responses new in v1.4.2

Leaf Core controller contains methods to appropriately return data to the user.

respond

respond returns json encoded data to the user.

use Leaf\Core\Controller; class NameController extends Controller { public function index() { $this->respond([ "message" => "hello" ]); } }

respondWithCode

respond returns json encoded data to the user along with an HTTP code.

use Leaf\Core\Controller; class NameController extends Controller { public function index() { $this->respondWithCode([ "message" => "hello" ], 200); } }

View Response Docx for all Response methods

Veins

Veins simply help you pass data from the controller to the view, Vein itself can serve as a view layer on it's own.

configure

Basic configuration for veins

use Leaf\Core\Controller; class NameController extends Controller { public function index() { $this->veins->configure([ "veins_dir" => "app/views/", "cache_dir" => "app/views/build/" ]); } }
veins_dir refers to the directory in which your veins(View Layer Code) are kept
cache_dir refers to the directory in which compiled veins(View Layer Code) are kept

set

Pass information to your view. It takes in one parameter, an array ofitems to pass into the view

use Leaf\Core\Controller; class NameController extends Controller { public function index() { $this->set([ "name" => "Mychi" ]); } }

You can access the name value passed into the view:

Hello {$name}

render

Outputs a vein(view)

use Leaf\Core\Controller; class NameController extends Controller { public function index() { $this->set([ "name" => "Mychi" ]); $this->render("index"); // refers to index.vein in the veins_dir } }
veins_dir refers to the directory in which your veins(View Layer Code) are kept

View Veins Docx for all Vein methods

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\Controller; class NameController extends Controller { 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\Controller; class NameController extends Controller { public function index() { $obj = $this->objectify([ "name" => "Mychi" ]); } }

Next Steps

Simple Routing
Response
Simple Authentication
Database Config