Slim 4 - Framework vs. Micro Framework

Daniel Opitz
Daniel Opitz
22 Jan 2021

Table of contents

Introduction

Over the last few years, I’ve noticed that there’s a lot of confusion (and false expectations) around the Slim micro framework. In the following chapters, I will try to explain the conceptual differences between full-stack frameworks and micro frameworks. You should not consider this comparison as “Slim vs. Laravel”, but rather as framework vs. micro-framework in general.

Micro Framework

A microframework is a term used to refer to minimalistic web application frameworks. It is contrasted with full-stack frameworks.

According to Wikipedia a micro framework typically…

… facilitates receiving an HTTP request, routing the HTTP request to the appropriate controller, dispatching the controller, and returning an HTTP response.

Micro frameworks are often specifically designed for building the APIs for another service or application. For example, the Slim micro framework is designed for Microservices development and APIs.

It lacks most of the functionality which is common to expect in a full-fledged web application framework, such as:

Full-stack Framework

Web frameworks provide a standard way to build and deploy web applications on the web.

Many web frameworks provide libraries for database access, template engines, and session management, and they often promote code reuse.

Although they often target development of dynamic websites, they are also applicable for web APIs.

Laravel and Symfony belong to the group of full-stack frameworks.

To be fair, Symfony has been rebuilt in recent years to the point where Symfony Flex could almost considered a microframework approach as well.

Slim

Slim is a micro framework.

Although the idea behind Slim is relatively simple, it still requires a lot of explanation.

You might know this Unix philosophy:

Make each program do one thing well.

According to this philosophy the Slim core is just a “routing library”. Other things like Environment loading, configuration, database abstractions etc. is a different set of concerns that other packages do very well and there’s no reason for Slim to reinvent that wheel.

It may sounds simple, but in reality it’s not as easy as you might think. Depending on your technical background, it may take a long time to understand and successfully use this tool.

What is routing?

Routing is the process of taking a URI path and decomposing it into parameters to determine which module, controller, and action of that controller should receive the request. Routing occurs only once: when the request is initially received and before the first controller is dispatched.

In Slim all this happens when you add a route as follows:

<?php

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;

$app = AppFactory::create();

$app->get('/', function (Request $request, Response $response) {
    $response->getBody()->write('Hello, World!');
    
    return $response;
});

$app->run();

Output:

Hello, World!

Evaluating Slim

Besides the technical requirements, developers should also be “fit” enough for Slim. Here is a list of resources you should already be familiar with before evaluating Slim.

Minimum skill set for Slim:

If you find something you feel not familiar with, please learn it before you try Slim. But that was just the beginning. You should also have a stable knowledge of security, design patterns and all SOLID principles to build secure and maintainable software.

Read more: Architecture

Is Slim the right tool for me? When should I consider Slim and when not?

You might consider a micro framework like Slim if you:

You might consider a full-stack framework if you:

Community

Compared to Laravel and Symfony, the Slim community is actually very small.

Good support is very important, especially for beginners. The Laravel and Symfony community is very big and willing to help as fast as possible. They also provide tons of tutorials, videos, online courses and certificates.

Especially beginners struggle to understand Slim and think it’s “simple” and so it must easy for them. This is not true. Slim is simple but not easy. Slim cannot be learned in an “afternoon”. Also PHP and development in general cannot be learned in an afternoon. This leads to a strange situation, because most support requests don’t concern Slim itself. In the last few years, most of these support issues are about teaching people how to configure their webserver, especially Apache with mod_rewrite. The second most common questions are about basic PHP knowledge such as classes, namespaces, and autoloading (PSR-4). What I try to say is: Slim is not for beginners.

Bundles, Packages and Plugins

A general-purpose framework can integrate other plugins or bundles based on a fixed structure.

In Slim, however, such an ecosystem cannot emerge because this “plugin mechanism” is missing. In Slim however each individual component can be integrated with the help of “Composer” and some DI container definitions. This is more complex, but also more independent and flexible.

Standards

The PHP-FIG is a group of experts trying to find ways for us to work better together. For this reason, this group has also developed various specifications for frameworks such as PSR-7, PSR-15 etc. Unfortunately, the big frameworks like Laravel and Symfony are not yet ready to implement these standards, probably to strengthen their own ecosystem (a.k.a. vendor lock-in).

On the other hand, Slim (and Laminas) provides full support for most standard PSR interfaces to ensure very good interoperability between other packages. This ensures that you are able to replace the HTTP message or DI container package “relatively” easily without risking too much dependency on a particular vendor.

Conclusion

I think that Slim does exactly what a micro framework should do: routing. The DI container is optional, but recommended. If that’s still too much or too magical for you, you may take a look at nikic/FastRoute.

Everything is relative, even complexity. Everything in life has its advantages and disadvantages. We have to find the right balance. Everyone must decide for themselves what suits them better and what does not. Luckily, we have the freedom to decide this for ourselves.