Skip to content
On this page

Leaf API

Leaf API is a minimal but powerful PHP MVC framework. It's designed to be simple, fast and easy to use. Leaf by default doesn't give you a lot of structure, and that's where Leaf API comes in.

Leaf API is a setup that gives you a good starting point for building APIs using the MVC pattern. It's built on top of Leaf, and comes with additional tooling that make building with Leaf even faster.

Installation

The easiest way to setup Leaf MVC is to use the Leaf CLI:

leaf create <project-name> --api

You can also setup a Leaf API app by using Composer:

composer create-project leafs/api <project-name>

This command will set up a Leaf API app in the <project-name> directory. You can then run the app using the Leaf CLI:

cd <project-name>
leaf serve

Or the built-in PHP server:

cd <project-name>
php -S localhost:8000

You should then see the welcome page in your browser.

Directory Structure

The Leaf API directory structure is inspired by Ruby on Rails and Laravel. It takes a lot of inspiration from these frameworks, but it's not a clone of either of them. It is meant to be a starting point for building your own applications, and is fully customizable. You can completely change the directory structure to suit your needs, just be sure to update the paths in the config.php file.

For a fresh Leaf API app, the directory structure looks like this:

C:.
├───app
│   ├── console
│   ├── controllers
│   ├── database
│   │   ├── factories
│   │   ├── migrations
│   │   ├── schema
│   │   └── seeds
│   ├── helpers
│   ├── models
│   ├── routes
│   └── views
├───config
├───public
├───storage
│   ├───app
│   │   └───public
│   ├───framework
│   │   └───views
│   └───logs
└───vendor
  • The app directory

    The app directory contains the core code of your application. It's divided into a few sub-directories:

    • console - Contains the console commands for your application. These are used to perform tasks on the command line.
    • controllers - Contains the controllers for your application. These are used to handle HTTP requests.
    • database - Contains the database related code for your application. This includes migrations, seeds, factories and schema.
    • helpers - Contains the helper functions for your application.
    • models - Contains the models for your application. These are used to interact with the database.
    • routes - Contains the routes for your application. These are used to map HTTP requests to controllers.
    • views - Contains the views for your application. These are used to render HTML responses.
  • The config directory

    The config directory contains the configuration files for your application. These are used to configure how Leaf and it's modules interact with your application. You can find more information about the configuration files in the Configuration section.

  • The public directory

    The public directory contains the entry point for your application, and it's also used to serve static assets. The index.php file is the entry point for your application. All requests are routed through this file by the web server. This file doesn't contain any application logic, but it does load the Composer autoloader, the application config and all your routes.

  • The storage directory

    The storage directory contains the compiled views, logs and other files generated by your application. It's divided into a few sub-directories:

    • app - Contains the files generated by your application. This includes the compiled views and the files uploaded by users.
    • framework - Contains the framework generated files for your application.
    • logs - Contains the log files generated by your application.
  • The vendor directory

    The vendor directory contains all the dependencies installed by Composer. It's automatically generated when you install the dependencies using Composer.

Next Steps

Follow along with the next steps to learn more about Leaf API.

Leaf API has loaded