Introduction
Slim is a PHP micro-framework for building APIs and small web applications. It provides routing, middleware, and PSR-7 HTTP message handling without the overhead of a full-stack framework. Slim is a good fit when you need a lightweight foundation and want to pick your own ORM, template engine, and libraries.
What Slim Does
- Routes HTTP requests to callable handlers using a fast, tree-based router
- Supports PSR-7 request and response objects for standards-compliant HTTP handling
- Provides a middleware stack for cross-cutting concerns like authentication and CORS
- Includes dependency injection via a PSR-11 compatible container
- Handles content negotiation, error rendering, and response streaming out of the box
Architecture Overview
Slim is built around the PSR-7 HTTP message interface and PSR-15 middleware dispatcher. Incoming requests are wrapped in a ServerRequest object, passed through a middleware stack, matched to a route, and dispatched to a handler that returns a Response. The framework ships with a default error handler, a routing middleware, and body-parsing middleware, but every component is replaceable.
Self-Hosting & Configuration
- Require via Composer:
composer require slim/slim slim/psr7 - Use the official skeleton project for a pre-configured directory structure
- Configure routes in
routes.phpor directly on the App instance - Add middleware globally or per-route for authentication, logging, or rate limiting
- Deploy behind Nginx or Apache with standard PHP-FPM configuration
Key Features
- Minimal footprint with no mandatory dependencies beyond PSR interfaces
- PSR-7 and PSR-15 compliance for interoperability with the PHP ecosystem
- Flexible dependency injection supporting any PSR-11 container
- Built-in error handling with JSON and HTML error renderers
- Route grouping and named routes for clean URL generation
Comparison with Similar Tools
- Laravel — full-stack framework with ORM, auth, and queues built in; Slim is lighter and lets you choose your own components
- Symfony — enterprise-grade component library; Slim offers faster bootstrapping for microservices and APIs
- Lumen — Laravel's micro-framework, now deprecated in favor of full Laravel; Slim remains independently maintained
- Mezzio (formerly Expressive) — PSR-15 middleware framework by Laminas; Slim has a simpler API and larger community
FAQ
Q: When should I choose Slim over Laravel? A: Choose Slim for APIs and microservices where you want minimal overhead and full control over your dependencies. Use Laravel when you need an integrated full-stack solution.
Q: Does Slim support middleware? A: Yes. Slim uses PSR-15 compatible middleware. You can stack middleware globally or on individual routes and route groups.
Q: Can I use Slim with an ORM like Eloquent or Doctrine? A: Yes. Slim does not include an ORM, but you can add Eloquent, Doctrine, or any other PHP ORM via Composer.
Q: What PHP version does Slim require? A: Slim 4.x requires PHP 7.4 or later. PHP 8.x is fully supported.