Introduction
Slim is a PHP micro framework designed for building APIs and small-to-medium web applications. It implements PSR-7 HTTP messages and PSR-15 middleware, giving developers a thin but standards-compliant layer between an HTTP request and response without the overhead of a full-stack framework.
What Slim Does
- Routes HTTP requests to callable handlers using a fast, tree-based router
- Implements PSR-7 request/response interfaces for interoperable HTTP handling
- Supports a middleware pipeline for cross-cutting concerns like auth and CORS
- Provides dependency injection via any PSR-11 compatible container
- Handles error rendering with customizable error handlers and formatters
Architecture Overview
Slim's core is a dispatcher that wraps a PSR-7 request through a middleware stack, matches the URI against registered routes, invokes the matched callable, and returns the PSR-7 response. The framework has no ORM, templating engine, or built-in auth — those are added via Composer packages, keeping the core under 70 KB.
Self-Hosting & Configuration
- Requires PHP 8.1+ and Composer
- Install with
composer require slim/slimplus a PSR-7 implementation - Entry point is typically
public/index.phpbehind Apache or Nginx - Configure via PHP arrays or a PSR-11 container; no YAML or INI files needed
- Deploy to any PHP-capable host, Docker, or serverless (AWS Lambda via Bref)
Key Features
- PSR-7 and PSR-15 compliance ensures interoperability with the PHP ecosystem
- Minimal footprint keeps startup time and memory usage low
- Flexible middleware architecture for layering functionality
- Route groups with shared middleware for clean API versioning
- Built-in support for content negotiation and response formatting
Comparison with Similar Tools
- Laravel — full-stack with ORM, queues, and auth; Slim is minimal and bring-your-own
- Symfony — enterprise component library; Slim is a single micro framework
- Lumen — Laravel-flavored micro framework; Slim is framework-agnostic and PSR-first
- CodeIgniter — lightweight but opinionated MVC; Slim has no view layer
- Mezzio (Laminas) — PSR-15 micro framework; Slim offers simpler API with less configuration
FAQ
Q: Is Slim suitable for large applications? A: Slim can power large apps when combined with a container, ORM, and templating engine, but a full-stack framework may be more productive at scale.
Q: How do I add templating?
A: Install a renderer like slim/twig-view or slim/php-view and register it in the container.
Q: Does Slim support WebSockets? A: Not natively. Slim handles HTTP request-response cycles; use Ratchet or Swoole for WebSockets.
Q: What changed in Slim 4? A: Slim 4 decoupled the PSR-7 implementation, adopted PSR-15 middleware, and requires an explicit PSR-11 container.