Introducing Leaf PHP 3
Tags: php, opensource, laravel, leafphp
Published: 2021-09-23 22:04:55

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/30ziyic4ih5nbbhn3fo3.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5gec8169g2w430j75alo.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7yk5qmih3i1rykrvd5db.jpg)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6gnm7fp3c2fmdu6kb00y.jpg)

- **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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ql4ho56inf7ojm542893.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nu1days9y4on7r21ufa1.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9aajbznqgqug49vaxywz.png)

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](https://github.com/leafsphp/leaf/tree/v3.x-dev). 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](https://twitter.com/leafphp)

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

```sh
composer require leafs/leaf dev-v3.x-dev
```

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

```php
<?php

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

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

app()->run();
```

You can run this with the built in php server

```sh
php -S localhost:5500
```

## 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