Leaf as a micro-framework
Use Leaf as a lightweight micro-framework with no structure to build simple applications and APIs.
Micro-frameworks are lightweight, minimal, and focused—giving you just what you need to build fast without the overhead of a full-stack framework. Leaf is built for simplicity, speed, and ease of use, while offering more functionality than most micro-frameworks.
Unlike Leaf MVC, using Leaf as a micro-framework gives you complete flexibility. There's no enforced structure, so you can build your app however you like—perfect for small projects and APIs that don’t need strict separation of concerns.
Getting started
You can create a new Leaf app using the create
command on the Leaf CLI.
leaf create my-app --basic
Installing without the CLI
If you don't have the Leaf CLI installed, you can install Leaf using Composer.
composer require leafs/leaf
You can then create a new Leaf app by creating a new index.php
file and requiring the Leaf autoloader.
<?php
require __DIR__ . '/vendor/autoload.php';
app()->get('/', function() {
response()->json(['message' => 'Hello, World!']);
});
app()->run();
Once you are in your application root, you can run the app using the serve
command.
php leaf serve
Your app is now running! Open http://localhost:5500
in your browser.
Building your first app
Now the fun begins! 🚀 With Leaf as a micro-framework, you have the freedom to build your app your way. Define routes, return JSON for an API, or use Blade to render views—it’s all up to you.
Here’s a quick example of a simple JSON API:
<?php
require __DIR__ . '/vendor/autoload.php';
app()->get('/', function() {
response()->json(['message' => 'Hello, World!']);
});
app()->run();
It’s a start, but there’s so much more to build! You can now expand your app by adding more routes and features.
As a maker, the fastest way to get started is by building a Coming Soon, Early Access, or Pre-Launch page. This gives you something real to share while you build—helping you attract early users and generate interest.
Let’s create a simple pre-launch page using Leaf as a micro-framework.
1 Setting up our views
We've already seen how routes work by returning JSON in the previous example. Now, let’s render a view using Blade instead. Unlike Leaf MVC, Blade isn’t installed by default when using Leaf as a micro-framework—but don’t worry, setting it up is quick and easy!
leaf install blade@v4
composer require leafs/blade:v4
Once Blade is set up, we can create a new view inside the views directory at the root of your app. By default, Leaf looks for views in this directory unless you configure a different path.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Coming Soon</title>
<style>
body { text-align: center; padding: 50px; }
h1 { font-size: 2.5rem; }
p { font-size: 1.2rem; color: #555; }
input { padding: 10px; width: 250px; }
button { padding: 10px 15px; background: #333; color: #fff; border: none; cursor: pointer; }
</style>
</head>
<body>
<h1>Something amazing is coming soon!</h1>
<p>Sign up to be the first to know when we launch.</p>
<form action="/store" method="post">
<input type="email" name="email" placeholder="Enter your email">
<button type="submit">Notify Me</button>
</form>
</body>
</html>
Now that we have a simple pre-launch page with a form to collect email addresses, we just need to create a route that will render this view.
<?php
require __DIR__ . '/vendor/autoload.php';
app()->get('/', function() {
response()->json(['message' => 'Hello, World!']);
});
app()->view('/prelaunch', 'prelaunch');
app()->run();
Here's what's happening behind the scenes:
app()
is a helper function that gives you access to the Leaf app instance. You can use it anywhere in your app.view()
is a method for defining a route that renders a Blade view. The first argument is the URL path, and the second is the name of the view file to load.
Notice we did not have to configure Blade because Leaf does that for you. You can now navigate to http://localhost:5500/prelaunch
to see your pre-launch page.
2 Handling the form submission
Now that we have a pre-launch page, we need to handle form submissions. Let’s create a new route to process the form and save the email to a database.
app()->view('/prelaunch', 'prelaunch');
app()->post('/store', function () {
// handle the email //
});
We used the post()
method to ensure only POST requests reach this route. The second argument is a function where we'll handle the email. Leaf makes validation simple—we can call validate()
on the request and define our validation rules. Let’s validate and store the email.
app()->post('/store', function () {
if (!$data = request()->validate(['email' => 'email'])) {
// validation failed, redirect back with errors //
return response()
->withFlash('errors', request()->errors())
->redirect('/prelaunch');
}
// save the email
});
That’s it for validation! But before we can save the email, we need to connect our database to the application. Let’s set that up next.
3 Setting up our database
We can set up our database by installing the database module and configuring our credentials. Just like we installed Blade earlier, we’ll install the database module the same way.
leaf install db
composer require leafs/db
Now we just need to fill out our database information:
<?php
require __DIR__ . '/vendor/autoload.php';
db()->connect([
'host' => '127.0.0.1',
'username' => 'root',
'password' => '',
'dbname' => 'Leaf',
]);
app()->get('/', function() {
response()->json(['message' => 'Hello, World!']);
});
app()->view('/prelaunch', 'prelaunch');
app()->post('/store', function () {
if (!$data = request()->validate(['email' => 'email'])) {
// validation failed, redirect back with errors
return response()
->withFlash('errors', request()->errors())
->redirect('/prelaunch');
}
// save the email
});
app()->run();
Creating your database
Unlike with Leaf MVC, Leaf as a micro-framework does not come with a database migration system. You will have to create your database manually. You can do this using a tool like phpMyAdmin, TablePlus or the command line.
CREATE DATABASE Leaf;
Once you have filled out your database information, you can now save the email to the database.
4 Saving the email
We can now save the email to the database.
<?php
require __DIR__ . '/vendor/autoload.php';
db()->connect([
'host' => '127.0.0.1',
'username' => 'root',
'password' => '',
'dbname' => 'Leaf',
]);
app()->get('/', function() {
response()->json(['message' => 'Hello, World!']);
});
app()->view('/prelaunch', 'prelaunch');
app()->post('/store', function () {
if (!$data = request()->validate(['email' => 'email'])) {
// validation failed, redirect back with errors
return response()
->withFlash('errors', request()->errors())
->redirect('/prelaunch');
}
db()->insert('emails')->params($data)->execute();
return response()
->withFlash('success', 'You have been added to our list!')
->redirect('/prelaunch');
});
app()->run();
You can use the withFlash()
method to send a message to the next request. This is useful for sending messages to the user after a redirect. We can now test our app by navigating to the /prelaunch
page and submitting an email.
5 Deploying your app
We have built a simple pre-launch page using Leaf. You can now deploy your app to a server using a service like Heroku, Fly.io a VPS like DigitalOcean, or even a shared hosting service like Sevalla.
What to read next
Now that you have built a simple pre-launch page, the next step is to get you familiar with the basics of building a full-stack application with Leaf. So you can build and launch your next big idea fast.
Routing
Learn more about routing in Leaf MVC, dynamic routes, middleware and more.
Handling Requests
Learn how to process incoming requests, handle form submissions, and more.
Using Databases
Learn how to build queries, build relationships, and interact with your database programmatically.
Frontend
Learn about SSR, SPA, and how to use Leaf with your favorite frontend framework.