Learn SLIM with Real Code Examples
Updated Nov 27, 2025
Explain
Slim is a micro-framework that provides just the essentials for routing, middleware, and request/response handling.
It follows PSR-7, PSR-15, and PSR-17 standards for HTTP message interfaces and middleware interoperability.
Includes routing, dependency injection, error handling, and middleware support out of the box.
Ideal for building RESTful APIs, single-page applications (SPA) backends, and small-to-medium web apps.
Its simplicity allows full customization and integration with other components or frameworks.
Core Features
Minimalist architecture with focus on HTTP handling
Flexible routing with route placeholders and named routes
Middleware stack for request/response modification
Integration with any templating engine or ORM
PSR-compliant HTTP messages and middleware interoperability
Basic Concepts Overview
App - the main Slim application instance
Route - defines a path and the callback/action
Middleware - intercepts request/response for pre/post-processing
Request - encapsulates HTTP request data
Response - encapsulates HTTP response data
Project Structure
public/ - web server entry point
src/ - application code (Controllers, Services, Middleware)
config/ - configuration files
logs/ - error and access logs
vendor/ - Composer dependencies
Building Workflow
Define routes with their callbacks or controllers
Apply middleware for authentication, logging, or caching
Inject dependencies via container or directly
Handle HTTP requests and generate responses
Test API endpoints or web pages
Difficulty Use Cases
Beginner: simple REST API with a few routes
Intermediate: SPA backend with middleware authentication
Advanced: microservice architecture with multiple services
Expert: custom middleware, dependency injection, and modular apps
Architect: integrating Slim with other frameworks or legacy systems
Comparisons
Slim vs Laravel - micro vs full-stack
Slim vs Symfony - lightweight vs enterprise features
Slim vs Lumen - minimalism vs Laravel micro-framework
Slim vs CodeIgniter - routing-focused vs lightweight full MVC
Slim vs Zend Expressive - PSR middleware-centric frameworks
Versioning Timeline
2010 - Initial Slim release by Josh Lockhart
2011 - Slim 2.x with improved routing and middleware support
2015 - Slim 3.x, fully PSR-7 compliant, middleware stack introduced
2019 - Slim 4.x, fully decoupled core, DI container support
2025 - Slim 5.x, enhanced PSR compliance, performance improvements
Glossary
App - main Slim application instance
Route - maps URI paths to callbacks
Middleware - modifies request/response
Request - HTTP request object
Response - HTTP response object