Skip to content

Leaf + MVC

Leaf is a lightweight PHP framework with a ton of loosely coupled libraries that can be used to build any kind of application. By default, Leaf doesn't give you a lot of structure, but it fully supports the MVC pattern without any extra configuration.

What is MVC?

MVC stands for Model-View-Controller. It is a pattern that separates your application into three distinct parts:

  • Models: These are the classes that represent your data. They are responsible for interacting with your database, and for validating your data.
  • Views: These are the files that are responsible for displaying your data to your user. They are usually written in HTML, but can also be written in other templating languages like BareUI or Blade or frameworks like Vue or React
  • Controllers: These are the classes that are responsible for handling the user's request, and for returning the appropriate response.
New to MVC?

If you're new to the MVC pattern, you can take a look at this video by Traversy Media that explains the MVC pattern, how it works and how it works in real-world applications.

MVC in Leaf

Leaf MVC is a minimal yet powerful setup for building applications with the MVC pattern. It extends Leaf with additional tools and structure, making development faster and more intuitive. With a clean, organized codebase, Leaf MVC is a great starting point for building scalable and maintainable applications.

Leaf MVC 4

Directory Structure

Leaf MVC’s directory structure is inspired by Rails and Laravel but remains lightweight and flexible. It’s a solid starting point, fully equipped with everything you need to build a modern web application.

A fresh Leaf MVC app follows this structure:

bash
├───app
   ├── controllers
   ├── database
   ├── models
   ├── routes
   └── views
└───public
    └───assets
        ├── css
        └── img
bash
├───app
   ├── controllers
   ├── database
   ├── models
   └── routes
└───public
  • app/ – This is where all your application logic lives, including controllers, models, views, and routes. Your database files also reside here.
  • public/ – Contains publicly accessible files like bundled CSS, JavaScript, and images. This is the only directory exposed to the browser.

There are also some folders that may be generated automatically by modules like the storage directory, which is used to store logs, cache, and other temporary files.

Configuring Leaf MVC

Leaf MVC works out of the box with minimal setup—most apps just need a few tweaks in the .env file, so it doesn’t include a config directory by default. When customization is needed, config files are organized by feature, making it easy to adjust settings without affecting others. To publish all default config files, run the following command:

bash
php leaf config:publish

This command will create the config directory in your app and copy all default config files, just like in earlier versions. You can also publish a specific config file while keeping the rest untouched:

bash
php leaf config:publish <config-file>

Here is a list of all available Leaf MVC config files:

Config fileUse-case
appConfiguration for core features
authConfiguration for authentication (requires auth module)
corsConfiguration for cors (requires cors module)
csrfConfiguration for csrf protection (requires csrf module)
databaseConfiguration for database stuff
mailConfiguration for mailing (requires mail module)
redisConfiguration for redis management (requires redis module)
queueConfiguration for queue management (requires queue module)
viewConfiguration for view rendering

Application Environment

Leaf MVC includes a .env.example file, which is copied to .env during installation. This file stores environment variables like database credentials, making it easy to configure different environments (development, testing, production). All values in .env are automatically loaded into the application, and you can access them using the _env() helper function. This function takes a key and an optional default value if the variable isn't set. Here's an example:

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

Be careful not to commit your .env file to your version control system as it contains sensitive information. We have already added the .env file to your .gitignore file so you don't have to worry about this.

Building with Leaf MVC

Although Leaf MVC is structured, it is still incredibly flexible, and offers you different ways to build your application. You can build a full-stack application using your favourite frontend tooling, or an extensive API using all the tools Leaf MVC provides. We have guides on how to build different types of applications with Leaf MVC, so you can choose the one that best fits your use-case.

MVC for Full-stack

Build full-stack applications with Leaf MVC.

Start building

MVC for APIs

Build APIs with a structured approach for better organization.

Start building

Released under the MIT License.