Install the CLI, create a project
Build with Leaf MVC โ
When your app needs separate places for routes, request handling, data, and templates, start with Leaf MVC. It uses the same Leaf APIs as the basic setup, with a directory structure for organizing your code.
Run it
Your app is live athttp://localhost:5500
Building a small API or a few pages? The basic setup is a simpler starting point.
Your first controller and route โ
Controllers group the methods that handle requests. Create one for users:
leaf g:controller usersThis creates app/controllers/UsersController.php. Its index() method can return a JSON response:
<?php
namespace App\Controllers;
class UsersController extends Controller
{
public function index()
{
return response()->json([
'message' => 'Hello from Leaf MVC'
]);
}
}Add a route in app/routes/index.php to call that method:
app()->get('/users', 'UsersController@index');Visit http://localhost:5500/users to see the response. Leaf matches the URL, calls UsersController::index(), and sends the JSON back to the browser.
Where your code goes โ
The main application folders are:
app/
โโโ controllers/
โโโ database/
โโโ models/
โโโ routes/
โโโ views/
public/Routes map URLs to controller methods. Controllers handle the request, use models to work with data, and return a response. For HTML pages, templates live in app/views/.
As your route list grows, you can split it into files such as _auth.php or _api.php in app/routes/. See MVC routing for route partials and middleware.
Add the features you need โ
Use models for database records, authentication for user accounts, and billing for payments and subscriptions. Each guide covers the setup and configuration for that feature.
For the frontend, you can render templates on the server or use a JavaScript framework. The frontend guide covers Blade, Inertia, and Vite integration.
Working with an AI assistant โ
Leaf CLI creates .leaf/CONTEXT.md with context about your project. Ask your coding assistant to read it before making changes, and point it to the feature you want to work on.
For example:
Read
.leaf/CONTEXT.md, then add a users endpoint. Follow the existing route and controller conventions, and use the project's user model.
If your assistant cannot access the project folder, run:
leaf contextPaste the output along with your request. See AI in Leaf for more on project context.
Deploy your app โ
Configure your production environment and point your web server at the public/ directory. The deployment guide covers hosting options and server setup.
If you get stuck, ask in the Leaf Discord server.
