PHP Web Framework Leaf PHP

Leaf PHP MVC + MariaDB
Leaf PHP MVC + MariaDB

Live stream set for 2025-08-20 at 14:00:00 Eastern

Ask questions in the live chat about any programming or lifestyle topic.

This livestream will be on YouTube or you can watch below.

Getting Started with Leaf PHP 4.2 MVC: A Simple App with MariaDB Integration

If you’re learning PHP and want to dive into modern frameworks without the heavy lifting of Laravel, then Leaf PHP 4.2 is an amazing choice. It’s a lightweight, expressive PHP framework that supports MVC, Blade templates, and integrates easily with databases — perfect for new developers and PHP veterans alike.

In this beginner-friendly post, I’ll walk you through how to set up a simple Leaf PHP app that connects to a MariaDB database and displays records from a people table in HTML using Blade.

Why Leaf PHP?

Leaf PHP is designed to be simple and fast. It offers:

  • MVC structure (Models, Views, Controllers)
  • Blade templating out-of-the-box
  • Easy integration with Eloquent ORM
  • Composer-based project setup

Whether you’re migrating from raw PHP or learning to build structured apps, Leaf gives you power without the bloat.

Prerequisites

Before we begin, make sure you have:

  • PHP 8.1 or later
  • Composer installed
  • MariaDB running locally (or remotely)
  • A database table called people with at least some sample data

Step 1: Install Leaf MVC

Let’s create a new Leaf MVC project using Composer:

composer create-project leafs/mvc leaf-app
cd leaf-app

This gives you a ready-to-go folder structure with MVC support, Blade templating, routing, and more.

Step 2: Configure the Database

Open the .env file and set your database credentials:

DB_HOST=127.0.0.1
DB_NAME=your_database_name
DB_USER=your_db_user
DB_PASS=your_db_password

Then, open public/index.php and ensure the database is initialized:

\Leaf\Database::initDb();

Step 3: Create the Model

Let’s define a model for our people table.

File: app/Models/Person.php

<?php

namespace App\Models;

use Leaf\Model;

class Person extends Model
{
    protected $table = 'people'; // Set the correct table name
}

Step 4: Create the Controller

Generate a controller:

php leaf g:controller People

Then edit it to fetch all rows from the people table and pass them to the Blade view.

File: app/Controllers/PeopleController.php

<?php

namespace App\Controllers;

use App\Models\Person;

class PeopleController extends Controller
{
    public function index()
    {
        $people = Person::all();

        response()->render('people', [
            'people' => $people
        ]);
    }
}

Step 5: Add the Route

File: app/routes/web.php

app()->get('/people', 'PeopleController@index');

Step 6: Create the Blade View

File: app/Views/people.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>People List</title>
</head>
<body>
    <h1>List of People</h1>

    @if (isset($people) && count($people) > 0)
        <ul>
            @foreach ($people as $person)
                <li>{{ $person->name }} - {{ $person->email }}</li>
            @endforeach
        </ul>
    @else
        <p>No records found.</p>
    @endif
</body>
</html>

Screenshots & Screencast

Leaf PHP Dependencies
Command Line Installation Of Leaf PHP Web Framework

Leaf PHP Server
Command Line Server Of Leaf PHP Web Framework

Leaf PHP Class Generator
Terminal Displaying Class Generator Result

Leaf PHP Route
Gnome Text Editor Displaying App Route File

Leaf PHP View
Gnome Text Editor Displaying App View File

Leaf PHP People Controller
Gnome Text Editor Displaying Custom People Controller
Leaf PHP People Model
Gnome Text Editor Displaying Custom People Model
Leaf PHP People Result
Web Browser Displaying Custom People Route Result

Leaf PHP Custom View Records In Web Browser

Learn More: My PHP Book & Course

This post is a great introduction, but if you’re serious about mastering PHP, I recommend:

Need Help? Work with Me 1-on-1

I offer one-on-one PHP tutorials, Leaf PHP project upgrades, and custom migrations. If you’d like personalized help or to work on your own Leaf PHP app:

Contact Me Here

Conclusion

Leaf PHP makes MVC development simple and clean. In just a few steps, you connected to a MariaDB database and displayed results with Blade — without needing to learn a huge framework.

Whether you’re just getting started or migrating from vanilla PHP, Leaf 4.2 is a lightweight but powerful option.

Happy coding! 😄

About Edward

Edward is a software engineer, web developer, and author dedicated to helping people achieve their personal and professional goals through actionable advice and real-world tools.

As the author of impactful books including Learning JavaScript, Learning Python, Learning PHP, Mastering Blender Python API, and fiction The Algorithmic Serpent, Edward writes with a focus on personal growth, entrepreneurship, and practical success strategies. His work is designed to guide, motivate, and empower.

In addition to writing, Edward offers professional "full-stack development," "database design," "1-on-1 tutoring," "consulting sessions,", tailored to help you take the next step. Whether you are launching a business, developing a brand, or leveling up your mindset, Edward will be there to support you.

Edward also offers online courses designed to deepen your learning and accelerate your progress. Explore the programming on languages like JavaScript, Python and PHP to find the perfect fit for your journey.

📚 Explore His Books – Visit the Book Shop to grab your copies today.
💼 Need Support? – Learn more about Services and the ways to benefit from his expertise.
🎓 Ready to Learn? – Check out his Online Courses to turn your ideas into results.

Leave a Reply

Your email address will not be published. Required fields are marked *