Slim 4 - Framework vs. Micro Framework
Daniel Opitz
22 Jan 2021
Table of contents
- Introduction
- Micro Framework
- Full-stack Framework
- Slim
- Evaluating Slim
- Community
- Bundles, Packages and Plugins
- Standards
- Conclusion
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:
- Database abstraction
- Web template engine
- Input validation and input sanitation
- Accounts, authentication, authorization, roles etc…
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:
- PHP
- HTTP Basics
- Linux File System Basics
- Apache Module mod_rewrite
- Dependency management with Composer
- The PSR-4 Autoloader
- PSR-7: HTTP message interfaces
- PSR-11: Container interface
- PSR-15: HTTP Server Request Handlers
- PSR-17: HTTP Factories
- Middleware
- Dependency injection
- Front controller
- MVC Model 2
- Debugging with Xdebug
- F12 - Developer Toolbar
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:
- meet the “minimum skill set for Slim” list (see above)
- need performance, efficiency, full control and flexibility
- feel experienced enough, and you know what you are doing
- really value standard interfaces like PSR-7 and PSR-15
- want to build long-lasting software
- want to prevent vendor lock-in
- have special requirements and may want to change packages
- want to follow industry standards and best practices (like dependency injection)
- want to build a “SOLID” software
- stay up to date with web development and security related topics
- know how to build and maintain complex software
- feel familiar with enterprise architecture concepts and patterns
You might consider a full-stack framework if you:
- cannot fulfill all the points in the above list
- need support from a big community.
- don’t care about vendor lock-in
- accept the risks and costs of upgrading to the next framework version
- don’t care about high performance response times (SEO)
- prefer to use bad and slow OOP/SQL abstractions like an (N+1) ORM
- prefer framework specific “bundles” or “plugins” over pure composer packages
- need tons of online resources like tutorials, videos, courses and certifications
- want to write something into your CV
- and your team need a rigid structure in order to function
- don’t have the time or want to take the risk to decide which component to use
- don’t want to learn something new, like the PSR interfaces and middleware concept
- don’t feel safe enough in terms of web security
- prefer to install a bunch of IDE plugins to make your framework usable
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.