Configs2026年7月20日·1 分钟阅读

Slim Framework — PHP Micro-Framework for APIs and Small Apps

A lightweight PHP framework for building fast APIs and web applications with PSR-7 HTTP messaging and middleware support.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Slim Framework Overview
直接安装命令
npx -y tokrepo@latest install fc258825-83d3-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

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.php or 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.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产