Leaf MVC 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 MVC comes in.
Leaf MVC is a setup that gives you a good starting point for building applications using the MVC pattern. It's built on top of Leaf, and comes with additional tooling that make building with Leaf even faster.
The easiest way to setup Leaf MVC is to use the Leaf CLI:
leaf create <project-name> --mvc
You can also setup a Leaf MVC app by using Composer:
composer create-project leafs/mvc <project-name>
This command will set up a Leaf MVC 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.
The Leaf MVC 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
For a fresh Leaf MVC app, the directory structure looks like this:
C:. ├───app │ ├── console │ ├── controllers │ ├── database │ │ ├── factories │ │ ├── migrations │ │ ├── schema │ │ └── seeds │ ├── helpers │ ├── models │ ├── routes │ └── views │ └── errors ├───config ├───public │ └───assets │ ├── css │ └── img ├───storage │ ├───app │ │ └───public │ ├───framework │ │ └───views │ └───logs └───vendor
appdirectory 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.
configdirectory 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.
publicdirectory contains the entry point for your application, and it's also used to serve static assets. The
index.phpfile 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.
There is also an
assetsdirectory found in the
publicdirectory. It contains the static assets for your application. These are served by the web server and are accessible to users.
storagedirectory 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.
vendordirectory contains all the dependencies installed by Composer. It's automatically generated when you install the dependencies using Composer.
Follow along with the next steps to learn more about Leaf MVC.