Menu

Basic Leaf App
Writing your first leaf app

Preface

Hello World

First of all, we need to setup our .htaccess file. See Re-routing to index.

To create a hello world project with Leaf, you simply need to initialise Leaf's Router(Leaf)

Leaf simply takes all requests coming into the application and handles them based on rules you define. Enough talk, let's get dirty.

index.php

require_once __DIR__ . "/vendor/autoload.php"; $leaf = new Leaf\Core\Leaf(); $leaf->get('/', function() { echo "Hello World"; }); $leaf->run();

That's all. It's this simple

Setup

You can download the leaf starter here.

Download Zip

Rendering HTML

Basic HTML

Since Leaf doesn't restrict you from using your default PHP, you can use echo to output HTML code, like this:

echo "

Hello World

";

Though this method works, it is not recommended since the default ouput type for Leaf is JSON, for this reason, Leaf has prepared a Response object with a bunch of handy methods(view Response for more info)

This Response object contains a couple of methods for handling HTML properly with the right headers...let's take a look

RenderMarkup()

This method simply renders any html passed into it with a content-type of text/html

require_once __DIR__ ."/vendor/autoload.php"; use Leaf\Core\Leaf; use Leaf\Core\Http\Response; $leaf = new Leaf(); $response = new Response(); $leaf->get('/', function() use($response) { $response->renderMarkup("

HTML Code

"); }); $leaf->run();

RenderHtmlPage()

This method simply displays an already created html/PHP page with the appropriate content type. This method is especially suited to server side routing.

require_once __DIR__ ."/vendor/autoload.php"; use Leaf\Core\Leaf; use Leaf\Core\Http\Response; $leaf = new Leaf(); $response = new Response(); $leaf->get('/', function() use($response) { $response->renderHtmlPage('./link/to/page'); }); $leaf->run();
You can read more on responses here

Next Steps

Simple Routing
Response
Request
Database