DEV Community

Cover image for Introducing Leaf PHP 3
Michael Darko
Michael Darko

Posted on • Updated on

Introducing Leaf PHP 3

It's been a while since the release of Leaf v2 last year. v2 brought in a whole lot of improvement to the Leaf franchise and opened up doors for further development and the reform of Leaf API and Leaf MVC. Leaf 2 has without a doubt been very successful and we'll like to thank everyone who has checked Leaf out or built anything with it πŸ™πŸ½

Why another major version?

Since Leaf 2 is doing really well, with v2.6 released a few days ago, why would we want to move on to a new major version? There are a bunch of reasons for this which I'll go over below.

  • Leaf's vision

image

This discussion above states some of Leaf 2's features and how easy it is to use them in your app. Leaf 2 provides a ton of features and unparalleled simplicity to match that, however...

image

Leaf 2 bloated without even realising it. Although Leaf 2 is still one of the most lightweight frameworks out there, it has a lot going on, and has features people don't use in everyday development so we decided to strip off those features and return Leaf to a clean simple state where other features will be served as installable plugins.

  • A fresh start

Considering all the goodies that would come out of this new version with the introduction of modules, source code of about 60kb, blazing speed, component rewrites and so much more, we decided to use this opportunity to refresh Leaf's identity and we did this thoroughly πŸš€

Leaf comes in with a new logo which looks simpler (going back to our vision) and a dark theme showing usability. Leaf 3 takes usability and simplicity to a whole new level without sacrificing any of the powerful goodies you need πŸ˜‡

Leaf 3

What's new in Leaf 3?

We can sing about Leaf 3's features for a full day. I'll list some of them below.

Leaf 3 New 1

  • Modules πŸ₯ž

This is the biggest change in Leaf since v2. Modules are basically wrapped up pieces of functionality which can be installed independently both in and outside Leaf apps. Modules are fast, small and easy to get started with. Modules also require zero configuration and can be used right after installation with zero setup.

  • Tiny Footprint 🦢🏾

Well, I doubt there's any other feature rich PHP framework with a source code size of 60kb with modules less than 5kb in size. This goes a long way to improve the performance of your applications.

  • Zero Configuration βš™οΈ

Another big deal leaf has been pushing is zero config. We believe that if you need to configure anything in your app, it should be external (like database connections and env). Leaf just works and Leaf 3 makes sure that no extra configuration is needed to make your app work, no matter what environment you're in or the modules/external libraries you pull in.
We believe that if it works on localhost, it must work in production (let's burn docker downπŸ˜‚)

  • Speed to production πŸš€

Leaf 3 and it's modules come with a ton of regularly used features which have been simplified for use, this means that the time spent building these features is greatly reduced. How fast you go from development to production will depend on how fast you can type🀭

  • Overpowered Efficiency ⏰

Leaf 3 helps you write clean and more modular code, with the basic Leaf setup having only 6 lines of code. This can be reduced further to 5 lines in "functional mode". Read below for more informationπŸ₯°

  • Super easy to learn πŸ‘¨πŸΎβ€πŸ«

Leaf 3 compared to leaf 2 is a lot easier to learn. This is because Leaf 3 has less at the core and anything else can be learned as a plugin. Because of this you can focus on learning what you need one thing at a time beginning with only what you need to develop your application.

  • Global Shortcut functions 🌍

Global functions is an idea taken from Leaf MVC and Leaf API. These functions create global initialisers for your app which makes sure that you don't go round repeating functions and long class namespaces.

Instead of Leaf\Http\Response::json($something), you can do json($something), response($something) or response()->json($something)

  • ??? β˜ƒοΈ

Adding global functions paved way for new features like what we call "functional mode". Look at this example below:

image

With functional mode, you don't even need to initialise leaf yourself, just install leaf and build your app without any worries. To top it all, installing modules like session opens up the door to more functions like session(), flash(), auth()...

Why modules?

This is a question a couple of people have asked me. I thought I would just answer this in case others were wondering.

  • Decluttering leaf core πŸ—ƒ

As mentioned above leaf blew up without realising. This lead to people having code and dependencies they would never use in their projects. The sad part is that some of this code would be executed without their knowledge eventually slowing down their app (leaf 2 is still blazing fast thoughπŸš€). For this reason, we decided to serve Leaf's base and provide modules that could be used to extend Leaf's power on demand.

  • The community asked for it πŸš΄πŸ½β€β™‚οΈ

image

Other users have been asking for this as far back as January this year. Last month, we made a twitter poll and the results we obvious.

image

Most of the people who contributed in this poll agreed that modules are the way forward.

  • Easy to version πŸ”–

Modules are independent libraries and so have their own versions, bugs and fixes. This means that you can build your app with Leaf and not have to worry about future changes throughout the whole framework. If you need a patch just install the latest version of that particular module instead of the full framework.

  • Easy to learn πŸ“š

Most modules (especially the newer ones) focus on providing a familiar and very friendly interface similar to other popular libraries and frameworks. For instance leaf cors is modelled after the express js cors middleware which is really popular. This means that if you've used express before jumping on this module would take less than a minute.

  • Modules can be used outside leaf 🌐

This is another ground shaking addition to the Leaf community. You can now take any piece of Leaf and use it in a totally unrelated framework or simply in a raw PHP project. Leaf modules are framework agnostic and will work anywhere there's PHP to run them.

The fun part is that all modules are plug-n-play, requiring zero configuration. You install, you use.

Release Info

Leaf 3 is now under active development under the v3.x-dev branch. A release data for v3 alpha has not yet been confirmed, but the progress on development is looking great with 21 modules already published on composer.

Updates during development will be posted on our twitter page

To keep up with our development, you can install leaf from the v3.x or v3.x-dev branches. Since all dev and pull requests are made to the v3.x-dev branch, this branch will receive more updates with early testing for features even after the alpha release.

To test v3 right now, you can set it up with composer

composer require leafs/leaf dev-v3.x-dev
Enter fullscreen mode Exit fullscreen mode

This will quickly setup leaf 3 with the default modules. From there, create your index.php file and add this quickstart.

<?php

require __DIR__ . "/vendor/autoload.php";

app()->get("/", function () {
  response(["name" => "Mychi"]);
});

app()->run();
Enter fullscreen mode Exit fullscreen mode

You can run this with the built in php server

php -S localhost:5500
Enter fullscreen mode Exit fullscreen mode

What of Leaf v2?

Leaf v2 will continue to maintained and will receive updates and security patches. You can still continue to use Leaf 2 in your existing projects since it will still receive security patches with no breaking changes. However, after the official release, we recommend going in for version 3

Top comments (1)

Collapse
 
llbbl profile image
Logan Lindquist

Nice Michael. Would love to have you write a blog post for PHPops.