Slim Framework
- Summary
-
Discussion
- Why should I prefer Slim over so many other PHP frameworks?
- How's the performance of Slim?
- What are the key features of Slim?
- Could you share details of how Slim middleware work?
- Which are the packaged middleware in Slim?
- What's the application lifecycle in Slim?
- How can I create routes in Slim?
- What are some online resources to get started with Slim?
- Milestones
- Sample Code
- References
- Further Reading
- Article Stats
- Cite As
Slim is a minimalistic PHP framework for implementing APIs. At its core, we can see it simply as a "routing library". It receives HTTP requests, routes them to the correct callback functions, and forwards the HTTP responses. By being minimal, Slim is fast and efficient. Unlike fullstack frameworks, developers don't end up installing lots of features that they may never use.
Slim uses the concept of middleware. Each middleware can implement something specific. By chaining together many such middleware, complex web applications can be built. Many third-party middleware are available that implement authentication, security, view templating for generating HTML responses, caching, and more.
Slim is open source and maintained by a community of developers. It can be used on its own (server-side rendering) or used alongside frontend frameworks such as React or Vue (client-side rendering).
Discussion
-
Why should I prefer Slim over so many other PHP frameworks? Slim is a good choice if you're looking for "performance, efficiency, full control and flexibility". Slim supports standard interfaces such as PSR-7 and PSR-15. This prevents vendor lock-in. Slim framework plays well with good software practices including SOLID principles, design patterns, security principles and dependency injection.
Slim's easier to learn than fullstack frameworks such Laravel or Symfony. Fullstack frameworks provide many things out of the box: authentication, security, database integration, template engines, session management, and more. With Slim, these are outside the core framework. They're available as middleware from third-party vendors. Developers can install them via Composer only if and when their app needs them.
Slim is suited for building modern RESTful APIs and services where data is exchanged in JSON or XML formats. Complex apps can also be built since the core framework can be extended with middleware. Complete web pages can be rendered on the server side with templating engines such as Twig-View or PHP-View. This brings Slim into the domain of MVC frameworks. Developers have used Slim backend with Next.js frontend. SPAs with Slim and Vue.js are possible.
-
How's the performance of Slim? In a performance study from 2016, Slim v2.6 ranked fifth in terms of throughput (requests per second), peak memory usage and execution time. Phalcon and Ice were the best. In another study from 2017, Slim 3.0 ranked third behind Phalcon and CodeIgniter. It performed better than microframeworks Silex and Lumen.
Slim's routing is based on FastRoute, which optimizes the parsing of regular expressions via chunking.
-
What are the key features of Slim? The key features of Slim include these:
- Routing: HTTP requests are efficiently routed to mapped callbacks. Routes are matched using regular expressions that can also capture and pass parameters into the callbacks. A callback is usually a function or a method of a class.
- Middleware: Built-in or third-party middleware allow specific handling of requests and responses. Developers can also write custom middleware. The middleware approach is modular and flexible: middleware can be reordered or disabled if needed.
- PSR-7 Support: HTTP messages interfaces conform to PSR-7 specifications. Slim provides an implementation of this interface but developers are free to adopt any alternative implementation.
- Dependency Injection: This makes the app more testable or configurable. The interface is PSR-11 compliant.
-
Could you share details of how Slim middleware work? In Slim, middleware can be visualized as concentric circles. Middleware are called from the outermost to the innermost circles. At the center, the application executes and routing takes place. Subsequently, middleware are again processed but in innermost-to-outermost circles. Another useful visualization is the LIFO stack.
Suppose we have two middleware: authentication and caching. Authentication middleware is executed first. If authentication fails, app skips further processing. If authentication succeeds, then caching middleware is executed. If response exists in the cache, app uses the cache and skips the main callback processing. If cache doesn't exist, the main callback is executed that generates the response. Then caching middleware is called to store the response in the cache if necessary. Finally authentication middleware is called to complete or modify the response if needed.
Slim middleware deal with PSR-7 request/response objects and PSR-15 request handler object. Middleware can be added to the app object and thus potentially called with every request. Or it can be added to only specific routes, in which case such middleware are called after routing is done. In both cases, outermost middleware is added last.
-
Which are the packaged middleware in Slim? Default Slim installation comes packaged with a few middleware:
- Routing: Implements routing based on FastRoute but developers can choose to use a different implementation.
- Error Handling: Useful during development but also in production where errors can be logged for later analysis.
- Method Overriding: Process
X-Http-Method-Override
HTTP header. For example, aPOST
request withX-HTTP-Method-Override:PUT
header can be treated as aPUT
request. - Output Buffering: Append to the current response body by default but this can be changed to "prepend" mode.
- Body Parsing: When implementing APIs, response is often in JSON or XML. This middleware handles these formats.
- Content Length: Appends
Content-Length
header to the response.
-
What's the application lifecycle in Slim? A Slim app is first instantiated with the call to
Slim\App()
. It's common to do this via the factory methodAppFactory::create()
. For each of the HTTP methods (such as GET, POST, DELETE, etc.) routes can be defined. Then middleware are added. Finally, the application is executed by calling the app object'srun()
method.App middleware execute in outermost-to-innermost order. When it reaches the innermost layer, application despatches the HTTP request to its suitable route object. If this route has its own middleware, they're executed in the same outermost-to-innermost order. Then control passes from the innermost to outermost middleware. Finally, application serializes and returns the HTTP response.
-
How can I create routes in Slim? Slim framework allows us to define routes for various HTTP methods: GET, POST, PUT, DELETE, OPTIONS, PATCH. It's possible to define a route for all methods using
$app->any()
method or for multiple methods such as in$app->map(['GET', 'POST'])
.Each route method accepts a pattern that's used to match against the URI of the HTTP request. Placeholders within the pattern are delimited using
{}
. Moreover, placeholders can be regular expressions. Optional parts of the pattern are delimited using[]
. For instance,$app->get('/users[/{id:[0-9]+})
wold match "/users" (sinceid
part is optional) and "/users/23" (sinceid
is a valid positive integer).Each route method also accepts a callback, which is a PHP callable with three arguments: Request, Response and Arguments. The values captured by placeholders are passed as arguments. In the above example,
id
is accessed as$args['id']
. Callback can be specified as an anonymous function, a method defined in a class, a class that implements the__invoke()
method, etc.Routes can be grouped to make code more maintainable. Thus, URIs
/users/{id:[0-9]+}
and/users/{id:[0-9]+}/reset-password
can be grouped into/users/{id:[0-9]+}
. -
What are some online resources to get started with Slim? Beginners can start with the official Slim documentation. This introduces Slim with a simple example and then goes into the details. For latest updates, visit the Slim blog. To get help from the community, head to the discussion forum.
For step-by-step guides to developing APIs or apps with Slim, see the Slim Walkthrough and Rob Allen's Building Modern APIs with Slim Framework.
For ideas on how to organize a complex Slim-based app, see an example filesystem layout (slide 18) . Read about architecting a layered structure. A Slim v4 starter project is also available.
Curated list of PSR-15 middleware are available at psr15-middlewares and awesome-psr15-middlewares.
Those who wish to contribute the framework itself, can find Slim's codebase on GitHub.
Milestones
2010
2011
The Slim blog illustrates a simple "Hello World" example with the Slim framework. App is initialized with the call $app = new Slim()
. Routes are configured using $app->get()
, $app->post()
, and so on. Application execution is trigger with the call $app->run()
. All of this can be placed in the file index.php. Via Apache's .htaccess file, requests can be directed to index.php.
2012
2012
2013
2015
2019
Slim v4.0.0 is released. This release requires PHP 7.1 or newer. It supports PSR-15 compliant middleware. Framework is decoupled from built-in PSR-7 implementation. Thus developers can use alternatives such as Zend, Nyholm and Guzzle. Likewise, routing, error handling and response emitting implementations are decoupled from the framework core. Routing is via Slim\Middleware\RoutingMiddleware
. App factory is introduced. By March 2022, this evolves to Slim v4.10.0.
Sample Code
References
- Ahmed, Shahzeb. 2021. "The Best PHP Microframeworks on a Cloud Server." Blog, Cloudways, June 28. Update 2021-06-29. Accessed 2022-09-28.
- Allen, Rob. 2020. "Building Modern APIs with Slim Framework." Presentation, February. Accessed 2022-09-28.
- Boronczyk, Timothy. 2013. "Working with Slim Middleware." SitePoint, February 20. Accessed 2022-09-26.
- Casey, Keith. 2016. "Building APIs in PHP Using the Slim Micro Framework." Course, LinkedIn Learning, October 10. Accessed 2022-09-26.
- Hanselman, Scott. 2014. "HTTP PUT or DELETE not allowed? Use X-HTTP-Method-Override for your REST Service with ASP.NET Web API." Blog, February 5. Accessed 2022-09-28.
- Ice Framework. 2016. "Benchmark." Ice Framework, March 31. Accessed 2022-09-26.
- Januszewicz, Tomasz. 2022. "slim4-nextjs-starter." On BitBucket, August 20. Accessed 2022-09-29.
- Matt. 2022. "I've said it before, and I'll say it again..." Tweet, Web Dev with Matt, on Twitter, March 21. Accessed 2022-09-29.
- Middlewares. 2021. "psr15-middlewares." On GitHub, May 2. Accessed 2022-09-26.
- Nawaz, Shahroze. 2016. "“I Created Slim to Answer My Need for a Simple Development Tool,” Josh Lockhart Discusses Slim Framework and Photography." Blog, Cloudways, July 29. Updated 2017-05-10. Accessed 2022-09-28.
- Nix Solutions. 2017. "Comparative testing of PHP frameworks." Blog, Nix Solutions, June 16. Accessed 2022-09-26.
- Opitz, Daniel. 2021. "Slim 4 - Framework vs. Micro Framework." Blog, January 22. Accessed 2022-09-26.
- PHP Framework Interop Group. 2022. "PSR-7: HTTP message interfaces." PHP Framework Interop Group. Accessed 2022-09-26.
- Popov, Nikita. 2014. "Fast request routing using regular expressions." Blog, February 18. Accessed 2022-09-26.
- Savoldi, Paolo. 2019. "Why I choose Slim Framework for my PHP web development." Blog, On Medium, February 18. Accessed 2022-09-26.
- Sheina, Elvira. 2017. "Maintain Slim PHP MVC Frameworks with a Layered Structure." Toptal, March 30. Accessed 2022-09-26.
- Slim. 2011a. "Say Hello World with Slim." Blog, Slim Framework, September 15. Accessed 2022-09-28.
- Slim. 2011b. "How to organize a large Slim Framework application." Blog, Slim Framework, September 24. Accessed 2022-09-28.
- Slim. 2012a. "Slim Framework Version 1.6.0." Blog, Slim Framework, April 14. Accessed 2022-09-28.
- Slim. 2012b. "Use Slim Framework with Composer and Packagist." Blog, Slim Framework, April 22. Accessed 2022-09-28.
- Slim. 2012c. "Slim Framework 2." Blog, Slim Framework, September 13. Accessed 2022-09-28.
- Slim. 2013. "Slim Framework Version 2.3.0." Blog, Slim Framework, July 14. Accessed 2022-09-28.
- Slim. 2015. "Slim 3.0.0 released!" Blog, Slim Framework, December 7. Accessed 2022-09-28.
- Slim. 2019a. "Slim 4.0.0 released." Blog, Slim Framework, August 1. Accessed 2022-09-28.
- Slim. 2019b. "Slim 3.12.3 released." Blog, Slim Framework, November 28. Accessed 2022-09-28.
- Slim. 2019d. "Routing Middleware." Documentation, v4, Slim Framework, August 1. Accessed 2022-09-26.
- Slim. 2019e. "Method Overriding Middleware." Documentation, v4, Slim Framework, August 1. Accessed 2022-09-26.
- Slim. 2019f. "Output Buffering Middleware." Documentation, v4, Slim Framework, August 29. Accessed 2022-09-26.
- Slim. 2019g. "Body Parsing Middleware." Documentation, v4, Slim Framework, December 23. Accessed 2022-09-26.
- Slim. 2020c. "Lifecycle." Documentation, v4, Slim Framework, April 7. Accessed 2022-09-26.
- Slim. 2021a. "Slim 4 Documentation." Documentation, v4, Slim Framework, April 26. Accessed 2022-09-29.
- Slim. 2021b. "Middleware." Documentation, v4, Slim Framework, October 5. Accessed 2022-09-26.
- Slim. 2021c. "Routing." Documentation, v4, Slim Framework, April 18. Accessed 2022-09-26.
- Slim. 2021d. "Error Middleware." Documentation, v4, Slim Framework, May 26. Accessed 2022-09-26.
- Slim. 2022. "Slim 4.10.0 released." Blog, Slim Framework, March 14. Accessed 2022-09-28.
- Slim. 2022a. "Homepage." Slim Framework. Accessed 2022-09-26.
- Slim. 2022b. "PSR-7 and Value Objects." Documentation, v4, Slim Framework, January 23. Accessed 2022-09-26.
- Slim. 2022c. "Content Length Middleware." Documentation, v4, Slim Framework, April 22. Accessed 2022-09-26.
- Slim. 2022d. "Templates." Documentation, v4, Slim Framework, March 16. Accessed 2022-09-26.
- Slim Walkthrough. 2021. "Slim Backend." In: Slim Walkthrough, The Making of a Web Application. Accessed 2022-09-28.
- Tamada, Srinivas. 2014. "Create a RESTful services using Slim PHP Framework." Blog, 9Lessons.info, December 8. Accessed 2022-09-26.
- tpetersdorf.dev. 2021. "A single-page application (SPA) with Slim Framework and Vue.js." Blog, tpetersdorf.dev, January 11. Accessed 2022-09-29.
Further Reading
- Allen, Rob. 2020. "Building Modern APIs with Slim Framework." Presentation, February. Accessed 2022-09-28.
- Program With Gio. 2022. "Intro to Slim PHP Framework - Full PHP 8 Tutorial." Program With Gio, on YouTube, August. Accessed 2022-09-26.
- Boronczyk, Timothy. 2013. "Working with Slim Middleware." SitePoint, February 20. Accessed 2022-09-26.
- Jabnouni, Sahbi. 2020. "Clean Architecture for PHP & Slim framework." Blog, on Medium, July 28. Accessed 2022-09-26.
- Nawaz, Shahroze. 2017. "Using Eloquent ORM with Slim." Blog, Cloudways, August 8. Updated 2021-06-03. Accessed 2022-09-26.