Leaf is a PHP framework that helps you create clean, simple but powerful web apps and APIs quickly and easily.

Last update: Aug 6, 2022



Leaf PHP Framework



Leaf PHP

Latest Stable Version Total Downloads License

Leaf is a PHP framework that helps you create clean, simple but powerful web apps and APIs quickly and easily. Leaf introduces a cleaner and much simpler structure to the PHP language while maintaining it's flexibility. With a simple structure and a shallow learning curve, it's an excellent way to rapidly build powerful and high performant web apps and APIs.

Installation

You can easily install Leaf using Composer.

composer require leafs/leaf

This will install Leaf in your project directory.

Basic Usage

This is a simple demonstration of Leaf's simplicity. After installing Leaf, create an index.php file.

connect("host", "user", "pass", "db name"); // Base example $app->get("/", function() use($app) { $app->response()->json([ "message" => "Welcome!" ]); }); // Full login example $app->post("/auth/login", function() use($app, $auth) { $credentials = $app->request()->get(["username", "password"]); $user = $auth->login("users", $credentials, [ "username" => ["username", "max:15"], "password" => ["text", "NoSpaces", "min:8"], ]); if (!$user) { $app->response()->throwErr($auth->errors()); } $app->response()->json($user); }); $app->run(); ">

require __DIR__ . "vendor/autoload.php";

$app = new Leaf\App;
$auth = new Leaf\Auth;

$auth->connect("host", "user", "pass", "db name");

// Base example
$app->get("/", function() use($app) {
  $app->response()->json([
    "message" => "Welcome!"
  ]);
});

// Full login example
$app->post("/auth/login", function() use($app, $auth) {
  $credentials = $app->request()->get(["username", "password"]);

  $user = $auth->login("users", $credentials, [
    "username" => ["username", "max:15"],
    "password" => ["text", "NoSpaces", "min:8"],
  ]);

  if (!$user) {
    $app->response()->throwErr($auth->errors());
  }

  $app->response()->json($user);
});

$app->run();

You may quickly test this using the built-in PHP server:

php -S localhost:8000

You can view the full documentation here

Working With MVC

Leaf has recently added a new package to it's collection: LeafMVC. It's an MVC framework built with this package at it's core that let's you create clean, simple but powerful web applications and APIs quickly and easily.

Checkout LeafMVC here

Working with API

Leaf also added a simple framework constructed in an MVCish way, but without the View layer purposely for creating APIs and Libraries. Leaf terms this construct as MRRC(Model Request Response Controller ๐Ÿ˜… ๐Ÿ˜… ๐Ÿ˜… ). This let's you seperate API logic, data and "views"(request and response) just like how it's done in MVC.

Checkout the LeafAPI package here

Skeleton

Skeleton is the latest package included in the Leaf family. Skeleton is a customizable and simple to use boilerplate powered by Leaf. Skeleton gives you the power of other setups like Leaf MVC without the restrictions of those full blown frameworks. Use and contribute to Skeleton

Of course, with this core package, you can build your app in any way that you wish to as Leaf contains all the required functionality to do so

View Leaf's docs here

Built with โค by Mychi Darko

GitHub

https://github.com/leafsphp/leaf
Comments
  • 1. Fatal error: Uncaught InvalidArgumentException: Unsupported driver

    I am a Chinese user, can not speak English, or a PHP rookie, this is using Google automatic translation, I apologize first. I encountered an error using the model, What did I do wrong?

    Fatal error: Uncaught InvalidArgumentException: Unsupported driver

    My program code:

    App\Controllers\UserController.php

    namespace App\Controllers;
    
    use Leaf\Controller;
    use App\Models\User;
    
    class UsersController extends Controller
    {
        public function index()
        {
            $users = User::all();
            foreach ($users as $user) {
                echo $user->name;
            }
        }
    

    App\Models\User.php

    namespace App\Models;
    
    use Leaf\Model;
    
    class User extends Model
    {
        protected $table = 'users';
        public $timestamps = false;
    }
    

    My database connection๏ผš

    $db = new Leaf\Db("localhost", "root", "root", "demo");
    
    Reviewed by wfsdaj at 2020-08-11 02:52
  • 2. Composer Doesn't Load

    v1.5.0

    Fatal error: Uncaught Error: Class 'Leaf\Core\Leaf' not found in /var/www/html/portal/index.php:58 Stack trace: #0 {main} thrown in /var/www/html/portal/index.php on line 58

    $leaf = new Leaf\Core\Leaf;

    All other composer packages in vendors are working fine.

    Reviewed by ideerge at 2020-02-11 17:36
  • 3. Nested mounts example

    How do they work? I keep getting ArgumentCountError Too few arguments to function {closure}(), 0 passed and exactly 1 expected

    $leaf->mount('/user', function() use ($leaf, $response, $twig) {
    
        $leaf->get('/', function() use($response) {
            echo $response->renderMarkup('<p>no user id</p>');
        });
    
        // will result in '/movies/id'
        $leaf->mount('/(\d+)', function($id) use ($leaf, $response, $twig) {
    
            echo $response->renderMarkup("<p>user {htmlentities($id)}</p>");
    
            $leaf->get('/', function() use($response) {
                //do something
            });
    
        });
    
    });
    
    Reviewed by aolko at 2019-10-24 09:11
  • 4. Response from GET Request Return as Null

    Hi there,

    We're following the main tutorial to obtain the GET parameter from the URL and output it to a JSON response.

    $app->get("/user", function() use($app) {
    
      $name = $app->request()->get("name");
      $app->response()->json([
        "name" => $name,
        "day" => "test"
      ]);
    
    });
    

    Expected response should be like this: curl -X GET http://localhost/user?name=test

    {
        "name": test,
        "day": "test"
    }
    

    But it keeps throwing out null as the response.

    {
        "name": null,
        "day": "test"
    }
    

    More information: php-7.4.16 / nginx 1.18.0

    Thank you.

    Reviewed by randytan at 2021-04-02 04:03
  • 5. Leaf Request error when sending json object as data

    It seems that Leaf Request doesn't handle json object as data. For example, with this payload: { name: toto age: 20 address: { name: 'my road' number: 18 town: 'Paris' } }

    I get this fatal error: Uncaught ErrorException: htmlspecialchars() expects parameter 1 to be string, array given in C:\wamp64\www\api.mecadrive\Leaf\Http\Request.php on line 242

    Is there any way to access these values with someting like: $request->get('address')->get('town'); // Paris or

    $request->get('address');
    $request['town']; // Paris
    

    Thank's for the job !

    Reviewed by chatelain at 2020-06-26 09:30
  • 6. Unable to start using application

    After fresh installation via composer leaf is not working. Using example code:

    <?php
    
    require __DIR__ . "/vendor/autoload.php";
    
    app()->get("/", function () {
      response()->json(["message" => "Hello World!"]);
    });
    
    app()->run();
    

    Getting:

    PHP Fatal error: Uncaught Error: Call to undefined function app()

    Paths are correct. Application just does not work. Using latest version: 3.0 RC2.

    P.S. Does the phpmailer should exist by default in 'vendor' directory if I did not install 'mail' module?

    Reviewed by ponasromas at 2022-02-23 07:11
  • 7. Database connection wrong documentation

    Hey guys, great framework. I just start to using it and must say, it is great job!

    The documentation using db module is wrong.

    $db->connect('127.0.0.1', 'username', 'password', 'dbname');

    Need to be:

    $db->connect('127.0.0.1', 'dbname', 'username', 'password');

    The other things working just fine, I'm testing and building my app on this.

    Thanks!

    Reviewed by InfinitumForm at 2022-06-09 07:44
  • 8. Request > Paths

    I'm looking at the "Paths" section here: https://leafphp.netlify.app/#/leaf/v/2.5.0/http/request

    As far as I can see the methods for "getRootUri" and "getResourceUri" have yet to be implemented? Is that correct?

    Reviewed by jamie-syon at 2021-07-12 16:45
  • 9. Is Leaf tested?

    Hello!

    Thanks for your work.

    My colleagues and I are willing to give Leaf a try. However I cannot see any tests. Is the framework tested in some ways?

    Reviewed by pabloelcolombiano at 2021-12-12 23:32
  • 10. Malformed JWT

    Describe the bug I just found out that there is a problem with the deformed JWT. The JWT library throws an error that must be caught.

    I found a problem in Authentication.php: 116 (version 2.X) You can fix it as follows.

    try { $payload = JWT::decode ($ token, $ secretPhrase, ['HS256']); if ($ payload! == null) return $ payload; } catch (\ DomainException $ exception) { self :: $ errorsArray ["token"] = "Malformed token"; }

    Thank you so much :-)

    To Reproduce Steps to reproduce the behavior:

    1. Send malformed JWT

    Expected behavior 40x response

    Reviewed by OtaHofmann at 2021-11-28 00:04
  • 11. Auth - register

    Hi,

    I did the registration through authorization and I get this error message.

    Can you help me with that?

    POST: { "username": "admin", "email": "[email protected]", "password": "s" }

    Error: { "error": { "dev": "Call to a member function bind_param() on bool" }, "code": 500 }

    Code:

    $app->post('/register', function () use ($app, $form, $auth) {
            $username = $app->request()->get("username");
            $email = $app->request()->get("email");
            $password = $app->request()->get("password");
    
            $validation = $form->validate([
                "username" => "username",
                "email" => "email",
                "password" => "required"
            ]);
    
            if (!$validation) $app->response()->throwErr($form->errors());
    
    
            $user = $auth->register("users", [
                "username" => $username,
                "email" => $email,
                "password" => $password
            ]);
    
            if (!$user) $app->response()->throwErr($auth->errors());
            $app->response()->json($user);
        });
    
    Reviewed by DominikJoppa at 2021-05-19 00:34
  • 12. _env not working on CLI

    Describe the bug

    The helper function _env() relies on $_ENV validation, preventing the correct use on CLI

    To Reproduce Steps to reproduce the behavior:

    1. Run the application using injected environments variables (like in docker and docker-compose and kubernetes) and skip the use of .env file (using .env files in clusters and containers is bad practice)
    2. debug the content of the container (docker) executing the command printenv to verify the variable you want has been injected in the container.
    3. Dump the content using the PHP function getenv() from inside leaf PHP (cli), The variable injected should be there, cool!
    4. Do the same but with _env()... Hmm, the variable is not there!

    Expected behavior _env() should read the environment variables just like getenv does.

    Additional context I love the simplicity and flexibility of this framework.

    What I'm doing is basically running the leaf framework inside a docker container to execute a command (not web), and because is docker, the .env files won't be used to handle environment variables, instead all the variables are injected to the container just like it works with any other framework.

    Unfortunately the presence of $_ENV in the validation inside _env function prevent the use of the variable even though the variable was captured by the official PHP function getenv().

    Reviewed by agarzon-orangerine at 2022-08-12 16:35
Simple PHP framework that helps you quickly understand and write simple APIs.

Lightweight-PHP-Framework-For-APIs. Simple PHP framework that helps you quickly understand and write simple APIs. Installation Use the package manager

Jul 22, 2022
Simple PHP framework that helps you quickly understand and write simple APIs.

Lightweight PHP Framework For Web and APIs PHP framework that helps you write quickly simple but powerful web apps and APIs Installation Use the packa

Jul 22, 2022
PhpBoot is an easy and powerful PHP framework for building RESTful/Microservices APIs.
PhpBoot is an easy and powerful PHP framework for building RESTful/Microservices APIs.

?? tiny & fast PHP framework for building Microservices/RESTful APIs, with useful features: IOC, Hook, ORM, RPC, Swagger, Annotation, Parameters binding, Validation, etc.

Aug 7, 2022
A powerful yet easy-to-use PHP micro-framework designed to help you build dynamic and robust Web applications - fast!
A powerful yet easy-to-use PHP micro-framework designed to help you build dynamic and robust Web applications - fast!

A powerful yet easy-to-use PHP micro-framework designed to help you build dynamic and robust Web applications - fast! Condensed in a single ~65KB file

Jul 30, 2022
Low-code Framework for Web Apps in PHP
Low-code Framework for Web Apps in PHP

Agile UI - User Interface framework for Agile Toolkit Agile Toolkit is a Low Code framework written in PHP. Agile UI implement server side rendering e

Aug 11, 2022
A magic PHP framework. Build reactive web apps without writing HTML, CSS, or JavaScript! Powered by Tailwind, Alpine, Laravel, & Livewire.
A magic PHP framework. Build reactive web apps without writing HTML, CSS, or JavaScript! Powered by Tailwind, Alpine, Laravel, & Livewire.

Malzahar A magic PHP framework. Build reactive web apps without writing HTML, CSS, or JavaScript! Powered by Tailwind, Alpine, Laravel, & Livewire. Re

Aug 15, 2022
Gracili is a PHP Application Template to quickly create a new Project

Gracili What is Gracili? Gracili is a PHP Application Template to quickly create a new Project. Using this template can save you a lot of time. With t

May 12, 2021
TrailLamp is a lightweight, easy-to-use Php MVC framework that can be used to build web applications and REST APIs.

TrailLamp Introduction TrailLamp is a lightweight, easy-to-use Php MVC framework that can be used to build web applications and REST APIs. Installatio

Jun 10, 2022
Bugsnag notifier for the Symfony PHP framework. Monitor and report errors in your Symfony apps.

Bugsnag exception reporter for Symfony The Bugsnag Notifier for Symfony gives you instant notification of errors and exceptions in your Symfony PHP ap

May 6, 2022
a framework for WebDevelop based on the mvc structure. The name of this project for Fun because everyone can use it. Completely simple and powerful structure for all your projects

A_A (-.-) โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„ |-| โ–ˆโ–„โ”€โ–„โ–„โ”€โ–ˆโ–„โ”€โ–ˆโ–ˆโ”€โ–„โ–ˆโ”€โ–„โ–„โ–„โ–„โ–ˆโ”€โ–„โ–„โ–„โ–„โ–ˆโ–„โ”€โ–ˆโ”€โ–„โ–ˆโ”€โ–„โ–„โ–„โ”€โ–ˆโ–ˆโ–€โ–„โ”€โ–ˆโ–ˆโ”€โ–„

Jun 29, 2022
This component may look complex, weird and full of hacks but it is a game changer for how we run PHP applications.
This component may look complex, weird and full of hacks but it is a game changer for how we run PHP applications.

PHP Runtimes In early 2021, Symfony created a "Runtime component". This component may look complex, weird and full of hacks but it is a game changer f

Aug 10, 2022
REST APIs using Slim framework. Implemented all CRUD operations on the MySql database
REST APIs using Slim framework. Implemented all CRUD operations on the MySql database

PHP REST API using slim framework and CRUD operations ?? Hi there, this is a simple REST API built using the Slim framework. And this is for the folks

Jun 1, 2022
Framework X โ€“ the simple and fast micro framework for building reactive web applications that run anywhere.

Framework X Framework X โ€“ the simple and fast micro framework for building reactive web applications that run anywhere. Quickstart Documentation Tests

Aug 9, 2022
A tiny, yet powerful, PHP micro-framework.

Equip Framework A tiny and powerful PHP micro-framework created and maintained by the engineering team at When I Work. Attempts to be PSR-1, PSR-2, PS

Jun 24, 2022
PHP Kafka client is used in PHP-FPM and Swoole. PHP Kafka client supports 50 APIs, which might be one that supports the most message types ever.

longlang/phpkafka Introduction English | ็ฎ€ไฝ“ไธญๆ–‡ PHP Kafka client is used in PHP-FPM and Swoole. The communication protocol is based on the JSON file in

Aug 12, 2022
FlyCubePHP is an MVC Web Framework developed in PHP and repeating the ideology and principles of building WEB applications, embedded in Ruby on Rails.

FlyCubePHP FlyCubePHP is an MVC Web Framework developed in PHP and repeating the ideology and principles of building WEB applications, embedded in Rub

Dec 21, 2021
Multi-process coroutine edition Swoole spider !! Learn about Swoole's network programming and the use of its related APIs

swoole_spider php bin/spider // Just do it !! Cache use Swoole\Table; use App\Table\Cache; $table = new Table(1<<20); // capacity size $table->column

Apr 22, 2021
A simple PHP MVC framework without extra files and codes that you don't need

Welcome to (SPM) Simple PHP MVC, just what you need! This is a simple PHP MVC framework without extra files and codes that you don't need.

Aug 4, 2022