Leaf MVC is a PHP MVC framework based on the Leaf PHP framework. It’s heavily inspired by Laravel and Ruby on Rails and tries to blend their good sides in one simple framework 😉.

In this article, we’ll be looking at a breakdown of LeafMVC and all it’s components(not into details😢). We’ll also be looking at a couple of tools provided by Leaf Core(Leaf PHP Framework🍀).

You can check out LeafMVC’s docs here, but don’t forget to read Leaf Core’s docs. Now that these are out of the way, let’s begin.

Installation

With LeafMVC, you can whip up a full MVC app in a couple of seconds. Leaf MVC’s recommended mode of installation is with composer.

composer create-project leafs/mvc <project-name>

This will create a LeafMVC project and install all dependencies. Also, a .env file will be created for you. You can serve your project with

php leaf serve

Folder Structure

C:.
├───app
│ ├───console
│ ├───controllers
│ │ └───Auth
│ ├───database
│ │ ├───factories
│ │ ├───migrations
│ │ └───seeds
│ ├───helpers
│ ├───models
│ ├───routes
│ └───views
│ ├───assets
│ │ ├───css
│ │ ├───images
│ │ └───js
│ ├───components
│ └───pages
│ └───errors
├───config
├───public
├───storage
│ ├───app
│ │ └───public
│ ├───framework
│ │ └───views
│ └───logs
└───vendor

In LeafMVC, app is where all your development goes on. “app” contains all your models, views(templates), controllers, routes, custom console commands, helpers and database related stuff(migrations, seeds…).

config contains all the app configurations…configuration for routes, database, the leaf console, error handling, session, templating…however, you normally have no business with this folder

public holds web configuration😅😅.

storage holds all the app data, it contains both framework data and user files. For instance, uploaded files and pictures are kept in storage/app/public

vendor holds all of leafMVC’s dependencies

Leaf Console

This is a simple tool to help with project management. With this tool, you don’t have to write your own models, migrations…from scratch. This console uses the php command line tool, so you should have php path defined.😉

Console Commands

In your console, you can run php leaf or php leaf list to view all supported commands. Alternatively, here are all supported commands:

LeafMVC Framework v1.3.0Usage:
command [options] [arguments]
Options:
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Available commands:
example Testing example command
help Displays help for a command
list Lists commands
serve Run your Leaf app
stalk Interact with your application
d
d:controller Delete a controller
d:model Delete a model
d:template Delete a template
db
db:migrate Run the database migrations
db:rollback Rollback all database migrations
db:seed Seed the database with records
g
g:controller Create a new controller class
g:helper Create a new helper class
g:migration Create a new migration file
g:model Create a new model class
g:template Create a new vein template
ui
ui:auth Scaffold basic login and registration views and routes

Building your first LeafMVC app

Note that all your development is done in the app directory. By default, a few demos have been created to give you a quick idea on how LeafMVC works.

Introduction

For this “tutorial”, we’ll be building a simple project to learn about Leaf in general. We’re going to be building a simple blog to demonstrate how LeafMVC works. We’ll be using models, views, controllers, migrations, leaf’s command line tool and a whole lot of other tools provided for us.😎

Note that this “tutorial” is divided into 2 articles. This article is for an introduction to LeafMVC and it’s components. The next section will be building the blog with LeafMVC.

Now that that’s out of the way, we can start with our actual development. When we take a look at our index.php file, we see that Leaf Core is initialised and our route config is also initialised.

Route Config

LeafMVC’s route config seperates routes into 2 categories: web routes and API routes. All routes which start with /api are considered as API routes by route config and are handled in app/routes/api.php all other routes are handled in app/routes/web.php in the case of this article, all routes will be handled in app/routes/web.php️ 🤷‍♂️

Basic Routing

We’ll be handling our routing in the web.php file. As mentioned before, LeafMVC runs on Leaf Core, routing and a whole lot of other cool features come directly from Leaf Core, so don’t forget to check out Leaf PHP Framework🤞. Now, let’s get started.

Leaf Core has already been initialised in our app as $leaf so, we can use all of Leaf’s functions on the $leaf object.

Let’s add the following code to our web.php file.

When we visit /home route in our browser, we see “This is the homepage”🔥. So basically, a route $leaf->method takes in the route uri and a callback function(callable/closure). This is a very efficient way to handle routes, however, it’s preferred to handle a route in a controller.

Using Controllers

The first thing we need to do to use a controller is obviously to create the controller. Our controllers are kept in app/controllers …you can manually create your controller in this directory, but there’s a better way😊. Remember we talked about the Leaf Console? We’re going to generate a controller now using the Leaf console tool. Open up your console and type:

php leaf g:controller PagesController

This will create a new file named PagesConroller in app/controllers and fill it in with a basic controller, clean right?😎👏. Let’s add some data to our controller.

Now to use this controller in our routes, we simply have to use at as the second parameter for our route

This will look in our PagesController for the index function. Though this works prooperly, it’s kind of a drag to type App\Controllers on every route, so we can use this instead

Note that there are 3 types of controllers available in LeafMVC:

  • Regular controllers(the controller we just generated)
  • Resource controllers(contains resource functions: php leaf g:controller --resource)
  • API controllers(tuned for APIs: php leaf g:controller --api)

Read more on controllers here😉

Rendering pages from controllers

LeafMVC allows you to quickly render pages from within controllers. Let’s consider the controller we generated, let’s say we want to render a page, we can simply do this with Leaf’s already defined render function.

What’s happening here in the render is quite simple. Leaf uses Leaf Veins just like how laravel uses blade. We are trying to render a vein template with the code above. $this->render("pages/index") here looks for a vein template named index in app/views/pages

Of course this won’t work right now, because the view index doesn’t yet exist, but we can create it simply with our leaf command tool

php leaf g:template pages/index

This will create index.vein.php in the app/views/pages directory. Simple and neat😎👌🔥. Now just go to the /home route and your vein template will be displayed.

Passing data into the view

With MVC, it’s very necessary to be able to pass data into your view from the outside, we’ll pass data into our index view from our PagesController.

To pass data into the view from our controller, we use set(); This is a special function used to pass data into a Vein Template. To set a single value, you can use set("key", "value"); , but it’s recommended that you use and array like so: set(["key" => "value"]);

To access the name variable which we passed into our index.vein.php view, we use this:

{$variable_key} is used to access a variable

Read more on Leaf Veins here

Styling

By default, bootstrap css and js files have been included in the app/views/assets directory. You can swap this out at any time by removing bootstrap.min.js and bootstrap.min.css

Model

To create a model you can use LeafMVC’s console tool

php leaf g:model <model_name>

This will create a simple model in app/models

You can also create a migration together with the model with

php leaf g:model <model_name> -m

With this, we have successfully created our first LeafMVC app. In the next article, we’ll be creating a simple blog with LeafMVC’s components.

View LeafMVC’s docs here

Don’t forget to check out Leaf’s docs