Skip to content

Leaf + MVC โ€‹

What is MVC? โ€‹

MVC stands for Model-View-Controller. It separates your application into the parts that hold data, display interfaces, and respond to requests.

01 / ModelData and rulesModels represent your data, query storage, and keep business rules close to the records they affect.
02 / ViewThe interfaceViews render HTML, Blade, BareUI, Inertia, React, Vue, or any frontend your product needs.
03 / ControllerRequest handlingControllers receive requests, call the right logic, and return responses without burying intent.
New to MVC?

MVC is a simple way to keep application code organized. Models talk to data, views present the interface, and controllers coordinate requests. Traversy Media has a useful overview if you want a broader introduction before building with Leaf.

Watch the MVC overview

MVC in Leaf โ€‹

Leaf MVC is a minimal setup for building structured applications. It adds the folders and commands most projects need, but avoids forcing your app into a heavy framework model.

Request flow

A route receives the request, a controller decides what should happen, models and services handle the work, and a view or JSON response returns the result. That predictable flow is why Leaf MVC works well for teams and AI-assisted changes.

Route receives request
app/routes/index.php
php
app()->get('/dashboard', 'DashboardController@index');
Controller coordinates work
Model or service handles logic
app/controllers/DashboardController.php
php
return response()->view('dashboard', [
  'data' => db()->select('frames')->get()
]);
View or JSON returns result
app/views/dashboard.blade.php
blade
<h1>Dashboard</h1>
<ul>
  @foreach ($data as $frame)
    <li>{{ $frame->name  }}</li>
  @endforeach
</ul>

Directory Structure โ€‹

Leaf MVC's directory structure is inspired by Rails and Laravel, but it stays lightweight and flexible. A fresh app starts with the places most product code naturally belongs.

Default starter

app/
controllers/
database/
models/
routes/
views/
public/

app/

Your controllers, models, views, routes, database files, and application logic.

public/

The only browser-exposed directory, usually for bundled CSS, JavaScript, and images.

Modules may also generate folders like storage for logs and cache, as well as temporary files.

Configuring Leaf MVC โ€‹

Leaf MVC works out of the box. Most projects only need a few environment variables, so there is no config directory until you publish one.

$ leaf config:publish <config-file>omit the name to publish every config
app

core app behavior

auth

authentication

database

database connections

view

view rendering

mail

mail delivery

queue

queued jobs

cors

cross-origin requests

csrf

CSRF protection

Application Environment โ€‹

Leaf MVC ships with a .env.example file that is copied to .env during installation. Values are automatically loaded and available through the _env() helper.

php
$database = _env('DB_DATABASE');
$databaseWithDefault = _env('DB_DATABASE', 'leaf');

Do not commit your .env file. Leaf MVC already adds it to .gitignore because it can contain database credentials, API keys, and other secrets.

Building with Leaf MVC โ€‹

Leaf MVC gives you structure without taking away your choices. Build a full-stack app, serve a frontend with Inertia or Blade, or expose a clean JSON API for any client.