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 provides a minimal but powerful setup for building applications using the MVC pattern. It's built on top of Leaf, and comes with additional tooling that makes building with Leaf even faster. It is a good starting point for building applications using the MVC pattern.
Installation
You can set up a new MVC application using the Leaf CLI or using Composer. They both work, but using the Leaf CLI gives you the option to choose between a regular MVC app and an MVC app that is fine-tuned for creating APIs, plus a few other options.
leaf create <project-name>
composer create-project leafs/mvc <project-name>
This command will set up a new MVC app in the <project-name>
directory. You can then run the app using the Leaf CLI:
cd <project-name>
leaf serve # or php leaf serve
You should then see the welcome page in your browser.
Directory Structure
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 config/paths.php
file.
For a fresh MVC app, the directory structure looks like this:
.
├───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
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. Each file controls a different feature of your application, e.g. theapp.php
file is used to configure the application, thedatabase.php
file is used to configure the database connection, etc.The
public
directory The
public
directory contains the entry point for your application, and it's also used to serve static assets. Theindex.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.There is also an
assets
directory found in thepublic
directory. It contains the static assets for your application. These are served by the web server and are accessible to users.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.
Configuring Leaf MVC
Leaf MVC tries to maintain a clean and easy-to-understand structure that works out-of-the-box for most applications. However, there are times when you need to customize some features to fit your specific use-cases.
You can find all the configuration options used by Leaf MVC in the config
directory of your Leaf MVC project. Each feature has its own configuration file, and you can customize these files to fit your needs, e.g. the database configuration file is config/database.php
.
You only need to change only the specific values you want to customize so you can leave the rest of the configuration as it is. Each option is documented, so feel free to look through the files and get familiar with the options available to you.
For the final bit, we hooked up most of the configuration files to your .env
file so you can easily change your configuration values without having to touch the configuration files directly. This is especially useful when you want to deploy your application to different environments.
Application Environment
A fresh Leaf MVC installation comes with a .env.example
file which is automatically duplicated to a .env
file on installation. This file is used to store your application's environment variables, and you can put sensitive information like your database credentials or mail server credentials in this file. This allows you to have different configurations for different environments like development, testing, and production.
Any value in your .env
file is automatically loaded into your application's environment variables, and you can access these values using the _env()
helper function. This function takes in the key of the environment variable you want to access and an optional default value if the environment variable is not set.
Here's an example of how you can use the _env()
helper function:
$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.