Skip to content

Installation

Leaf 3 is built by design to be incrementally adoptable. This means that it can be integrated into a project multiple ways depending on the requirements.

There are four primary ways of adding Leaf PHP to a project:

  1. Use the Leaf CLI to scaffold a project [RECOMMENDED].
  2. Download leaf through composer
  3. Scaffold a Leaf MVC or Leaf API project

Technical Requirements

Before creating your first Leaf application you must:

  • Install PHP 7.4 or higher and these PHP extensions (which are installed and enabled by default in most PHP installations): json, zip;
  • Install Composer, which is used to install PHP packages.
  • Optionally, you can also install Leaf CLI. This provides all the tools you need to create and manage your Leaf application locally. This is optional but highly recommended.
Not sure where to start?
  • Laravel released an amazing tool called Laravel Herd that provides a quick and easy way to set up a local PHP development environment for Mac. It's a great way to get started with PHP and Leaf.

  • On Windows and Mac, you can use Xampp, which is a free and open-source cross-platform web server solution stack package developed by Apache Friends, consisting mainly of the Apache HTTP Server, MariaDB database, and interpreters for scripts written in the PHP and Perl programming languages.

Leaf CLI

Video Docs

You can take a look at our leaf cli setup walkthrough on youtube.

Watch the leaf 3 installation walkthrough

Leaf provides an official CLI for quickly creating and managing your Leaf applications. It takes just a few seconds to get up and running with your leaf app. See the Leaf CLI docs for more details.

Using the CLI, you can quickly scaffold a new Leaf 3 project with:

leaf create <project-name>

Besides the core of the framework, Leaf also ships with a ton of installable functionality. We call these independent libraries modules. You can install modules using the install command:

leaf install <module-name>

The CLI also allows you to completely customize the installation you wish to create. You can choose different features like database, authentication, etc. This is done using the --custom flag:

leaf create <project-name> --custom

You can then run your app using the serve command:

leaf serve

Composer

Leaf also allows a more traditional approach to installation. You can install leaf through composer. You can use this method if you don't want to use the leaf cli or if you want to use leaf as a dependency in your project. The disadvantage of this method is that you don't get a quick-start setup like you do with the leaf cli.

composer require leafs/leaf

After insalling Leaf, you need to create your index.php file which will be the entry point to your application.

<?php

require __DIR__ . '/vendor/autoload.php';

$app = new Leaf\App;

$app->get('/', function () use($app) {
  $app->response()->json(['message' => 'Hello World!']);
});

$app->run();
<?php

require __DIR__ . '/vendor/autoload.php';

app()->get('/', function () {
  response()->json(['message' => 'Hello World!']);
});

app()->run();

When hosting your application on a webserver, all requests coming into your app must be routed through Leaf. It is really simple to do, and all needed instructions can be found @ URL rewriting.

MVC Setup

Although Leaf allows you to select exactly what you want to install, some applications go beyond the basic setup. Leaf MVC is a full but ridiculously light-weight MVC framework that allows you to build complex applications with Leaf. It comes with a lot of features like authentication, database, http related functionality and a powerful CLI. To get started with Leaf's MVC setup, you can check out the MVC docs.

Hello world example

Below is a "hello world" example which takes you through the core of Leaf. Other parts of the docs cover deeper examples. You can also refer to our codelab experiments for real world examples and use-cases.

A base Leaf app that outputs hello world in your browser looks like this:

<?php

require __DIR__ . '/vendor/autoload.php';

$app = new Leaf\App;

$app->get('/', function () {
  echo 'Hello world';
});

$app->run();
<?php

require __DIR__ . '/vendor/autoload.php';

app()->get('/', function () {
  echo 'Hello world';
});

app()->run();

You might have noticed that we used echo to output our data. Using echo is not a bad thing, but it can be confusing when you're trying to output data of a different type. For example, if we wanted to output JSON data, we would have to use echo json_encode($data). This can be confusing because we're not sure if the content type is set to JSON or not.

To simplify this, Leaf comes with a Response object that automatically handles content the right way for us. Let's look at an example below.

<?php

require __DIR__ . '/vendor/autoload.php';

$app = new Leaf\App;

$app->get('/', function () use($app) {
  $app->response()->markup('Hello world');
});

$app->run();
<?php

require __DIR__ . '/vendor/autoload.php';

app()->get('/', function () {
  response()->markup('Hello world');
});

app()->run();
Installation has loaded