New Amazing Features of Slim Framework


Are you a PHP developer who is tired of long texts of code and wants to try new frameworks? In this article, I am eager to introduce you to Slim, a PHP framework that is designed specifically for the fast development of APIs and web applications. No question, Symfony and Laravel are great tools as well, but sometimes you just need a minimum number of tools that will do anything you want. Here is where “micro” frameworks come in hand. “Micro” frameworks include a group of minimal requirements necessary for building a web application. Slim framework is a predecessor of the micro framework movement started in 2013 and is still one of the most popular frameworks today. But don’t be misled by its name. Slim comes with many valuable features dedicated to helping Slim developers, even in hardest situations.

Even though you can do almost anything with Slim, here are some of its features and advantages:

1. HTTP Router

2. Middleware

3. PSR-7 Support

4. Dependency Injection

5. Template rendering

6. HTTP Caching

7. Flash messages

8. Encrypted cookies

9. Logging

10. Error handling and debugging

11. Slack Integration

12. Create REST applications


At its core, Slim is a dispatcher that receives an HTTP request, invokes an appropriate callback routine, and returns an HTTP response.

So How Does it work?

To install this super-fast framework you need a web server like Apache. First of all, make sure that your.htaccess and index.php files are in the same public-accessible directory. Add this code to your .htaccess file.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

Check if your Apache virtual host is configured with the AllowOverride option.

AllowOverride All

Next, you should configure your web server, read the full documentation on their official website to not miss anything important. Meanwhile, if you are already a PHP guru here’s an example application:

// Create and configure Slim app
$config = [‘settings’ => [
‘addContentLengthHeader’ => false,
$app = new \Slim\App($config);

// Define app routes
$app->get(‘/hello/{name}’, function ($request, $response, $args) {
return $response->write(“Hello ” . $args[‘name’]);

// Run app

What are the Features of Slim Framework?

With version 3 released in 2014, progress is moving very quickly, and a lot of cool new features were introduced. The main goal of Version 3 was simplification and refactoring of the current processes within the framework. The new version is set to provide smaller and better-optimized codebase for Slim developers. Here are the new functions that were introduced.

  • Better-quality dependency injection and service location with Pimple
  • Improved cryptography
  • First-class session support
  • Initiate downloads from the file system or shell process output

The newly released 3.5.0 version of Slim framework included a ton of new features and improvements. Most notably, Slim started supporting media type structured suffixes (+json & +xml) for Request’s body parser and error response output. Routing, about which we will talk below, received several notable changes as well.

Here are several other functions and improvements that were added in the latest version. Check them all and tutorials for their implementation below:

1. Slack Integration

3.5 is the latest version currently available. Unlike version 3.4 it came just with several valuable bugfixes, plus the addition of Slack Integration. Slack is a web-based messaging software for organizing communication in teams. With the recent expansion of freelance culture and a daily increasing number of virtual companies, Slack became one of the most used tools worldwide. We are sure that Slack Integration will be a lifesaver for lots of slim developers.

2. HTTP Router

HTTP router is one of the main advantages and the reason Slim was created. It has a fast and powerful router that maps route callbacks to specific HTTP request methods and URIs. Slime supports parameters and pattern matching. Moreover, Slim’s routing supports HTTP verbs like “Update” “Delete,” “Post” and “Get” which allow programmers to create SEO optimized URLs.

Here is how to create Routes:

GET Route
Slim application’sget() method allows to add routes that only handle GET HTTP type requests.

$app = new \Slim\App();
$app->get(‘/books/{id}’, function ($request, $response, $args) {
// Show book identified by $args[‘id’]

POST Route and DELETE Route
The same works also for Post HTTP and Delete HTTP requests.

$app = new \Slim\App();
$app->post(‘/books’, function ($request, $response, $args) {
// Create new book

$app = new \Slim\App();
$app->delete(‘/books/{id}’, function ($request, $response, $args) {
// Delete book identified by $args[‘id’]

3. Middleware

For manipulating the Request and Response objects, a slim developer can write the code before and after the Slim application. This is the idea of middleware; it helps to protect applications for cross-site requests and authenticate requests before running applications. Building applications with middleware, tweak the HTTP requests and response objects around the Slim app.

So here is how middleware works: Slim adds middleware as layers around the core application. After that, the structure expands, adding new middleware layers. The last middleware layer added is the first to be executed.


Here is how you install and write closure middleware code:

“require”: {
“slim/middleware”: “*”

* Example middleware closure
* @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request
* @param \Psr\Http\Message\ResponseInterface $response PSR7 response
* @param callable $next Next middleware
* @return \Psr\Http\Message\ResponseInterface
function ($request, $response, $next) {

$response = $next($request, $response);

return $response;

PSR-7 Support

Be sure to check out this fantastic presentation by Matthew Weier O’Phinney for a better understanding of how PSR-7 and Middleware work, and how they are going to change the future of PHP.

Another advantage of Slim framework is that it supports any PSR-7 HTTP message implementation. It allows you to inspect and operate HTTP message method, status, URI, headers, cookies, and body. Slim works with its own PSR-7, but in case you want to, you are free to replace default objects with a third-party implementation.

  • FIG: Framework Interop Group
  • PSR: PHP Standards Recommendation
  • HTTP: HyperText Transport Protocol
  • CGI: Common Gateway Interface
  • SAPI: Server API

If you are just starting to enter into the depths of programming and have any issues, use the above terms to explain it faster. There are many humanist programmers who will help a colleague in need.

Dependency Injection

PHP Dependency Injection was introduced to Slim in Version 3. It allows the Slim developer to have full control over external tools, and to use any Container-Interop container. When you are building several routes, writing a code can be really messy. Watch this course about Controllers and Dependency Injection with Slim 3 to learn how to set up a controller, use classes, dependency injection, and use of the container.

Better to see and do it with your own hands, right?

Create REST Applications

Slim is also great for building a REST API, which makes it easy for developers to allow third-party access to application functions. Slim’s URL matching and routing features, alongside its lightweight API and support for HTTP methods, makes it perfect for rapid API prototyping.

HTTP Caching

Can you find a business that is not interested in ranking high on Google? unlikely! The HTTP Caching feature of Slim framework allows keeping assets which do not change frequently, like favicon, CSS files, static, and this improves a website’s overall performance and load time.

When I first saw all of the core features and advantages of Slim framework development, I was excited to dig deeper and find out anything necessary to use it in my own programming. Are you a Slim developer who is just getting started, or a person who just found about it? Here are tips from the creator of the frameworkJosh Lockhart himself.

His main advice is to spend more time digging into the HTTP protocol instead of focusing on the language itself. Clearly, having knowledge of PHP is a must, but knowledge of HTTP protocol will significantly advance a developer’s skills as a web application developer. This will advance ones’ ability and offer easy transfer to other languages as well.

Also, you can join PHP User Groups. Slim has a great community of users plus there are lots of fellow Slim developers to help you out and share their knowledge.

So to conclude:

1. Follow @slimphp on Twitter
2. Read the documentation
3. Join the Slim Framework support forum
4. Join the Slim ##slim Freenode channel on IRC

At the end of the interview, Josh also added that there are a lot of new slim features being discussed. Stay tuned to not miss any of the upcoming wonderful developments and features of Slim framework!

Leave a Reply

Your email address will not be published. Required fields are marked *