Slim 4 Package & Namespace Overview
Complete API Reference Catalog for Slim Framework v4.15.3 (slim/slim) — documenting all 72 public PHP files (55 classes and 17 interfaces) across 9 PSR-4 namespaces with strict type signatures, constructor/method parameter tables, and runnable PSR-7 / PSR-11 / PSR-15 / PSR-17 usage examples.
Source Code
from drawlib.canvas import save, setup
from drawlib.diagrams.architecture import ArchitectureDiagram, Node, NodeGroup, PhosphorIcon
from drawlib.styles import Styles
setup(width=148, height=82)
d = ArchitectureDiagram(
node_style=Styles.Dark,
node_text_style=Styles.DarkBold.patch(text_size=10.2),
edge_style=Styles.Dark.patch(line_width=1.4),
edge_text_style=Styles.Muted.patch(text_size=9.5),
node_card_style=Styles.Neutral,
)
boot_grp = d.add(
NodeGroup(
"1. Bootstrap Tier (12)",
width=38,
height=72,
style=Styles.PrimaryNeutral,
text_style=Styles.DarkBold.patch(text_size=10.8, halign="left", valign="top"),
),
xy=(5, 5),
)
n_factory = boot_grp.add(
Node(
"Slim\\Factory\n(2 Classes)",
icon=PhosphorIcon.FACTORY,
width=30,
height=17,
card_style=Styles.Neutral,
),
xy=(19, 48),
)
n_psr17 = boot_grp.add(
Node(
"Slim\\Factory\\Psr17\n(10 Classes)",
icon=PhosphorIcon.PLUGS_CONNECTED,
width=30,
height=17,
card_style=Styles.Neutral,
),
xy=(19, 18),
)
core_grp = d.add(
NodeGroup(
"2. Core & Routing Tier (22)",
width=42,
height=72,
style=Styles.SecondaryNeutral,
text_style=Styles.DarkBold.patch(text_size=10.8, halign="left", valign="top"),
),
xy=(53, 5),
)
n_root = core_grp.add(
Node(
"Slim\\ (Core)\n(5 Classes)",
icon=PhosphorIcon.CPU,
width=32,
height=15,
style=Styles.White,
text_style=Styles.WhiteBold.patch(text_size=10.2),
card_style=Styles.PrimaryFlat,
),
xy=(21, 53),
)
n_mw = core_grp.add(
Node(
"Slim\\Middleware\n(6 Classes)",
icon=PhosphorIcon.STACK,
width=32,
height=15,
card_style=Styles.Neutral,
),
xy=(21, 33),
)
n_routing = core_grp.add(
Node(
"Slim\\Routing\n(11 Classes)",
icon=PhosphorIcon.GIT_BRANCH,
width=32,
height=15,
card_style=Styles.Neutral,
),
xy=(21, 13),
)
err_grp = d.add(
NodeGroup(
"3. Handlers & Contracts (38)",
width=38,
height=72,
style=Styles.Neutral,
text_style=Styles.DarkBold.patch(text_size=10.8, halign="left", valign="top"),
),
xy=(105, 5),
)
n_handlers = err_grp.add(
Node(
"Slim\\Handlers\n(5 Classes)",
icon=PhosphorIcon.SLIDERS,
width=30,
height=13.5,
card_style=Styles.PrimaryNeutral,
),
xy=(19, 55),
)
n_renderers = err_grp.add(
Node(
"Slim\\Error\n(5 Classes)",
icon=PhosphorIcon.FILE_CODE,
width=30,
height=13.5,
card_style=Styles.Neutral,
),
xy=(19, 39),
)
n_exc = err_grp.add(
Node(
"Slim\\Exception\n(11 Classes)",
icon=PhosphorIcon.WARNING_OCTAGON,
width=30,
height=13.5,
card_style=Styles.Neutral,
),
xy=(19, 23),
)
n_ifaces = err_grp.add(
Node(
"Slim\\Interfaces\n(17 Contracts)",
icon=PhosphorIcon.SHIELD_CHECK,
width=30,
height=13.5,
style=Styles.White,
text_style=Styles.WhiteBold.patch(text_size=10.0),
card_style=Styles.AccentFlat,
),
xy=(19, 8),
)
n_factory.connect(n_psr17, label="discovers")
n_factory.connect(n_root, label="creates")
n_root.connect(n_mw, label="LIFO")
n_mw.connect(n_routing, label="routes")
n_mw.connect(n_handlers, label="errors")
n_handlers.connect(n_renderers)
n_routing.connect(n_exc, label="4xx/5xx")
n_routing.connect(n_ifaces, label="contracts")
d.draw(xy=(0, 0))
save()
Architecture & PSR Standards Compliance#
Slim v4.15.3 is architected around PHP-FIG (PSR) standards with declare(strict_types=1) enforced across 100% of its 72 source files (3,149 lines of code). Rather than bundling a monolithic HTTP message implementation or proprietary dependency injection container, Slim binds cleanly to any compliant PSR implementation:
| Standard | Composer Package | Version Constraint | Role in Slim v4.15.3 |
|---|---|---|---|
| PHP Runtime | php, ext-json |
~7.4.0 || ~8.0.0 .. ~8.5.0 |
Strict scalar type hints, return types, and JSON error/body serialization. |
| PSR-3 | psr/log |
^1.1 || ^2.0 || ^3.0 |
LoggerInterface injected into ErrorMiddleware and ErrorHandler, with Slim\Logger as fallback. |
| PSR-7 | psr/http-message |
^1.1 || ^2.0 |
Immutable ServerRequestInterface, ResponseInterface, and StreamInterface flowing through every middleware and route. |
| PSR-11 | psr/container |
^1.0 || ^2.0 |
Optional ContainerInterface resolved by AppFactory, CallableResolver, and MiddlewareDispatcher. |
| PSR-15 | psr/http-server-handler, psr/http-server-middleware |
^1.0 |
RequestHandlerInterface (App, Route, RouteRunner) and MiddlewareInterface (Slim\Middleware\*). |
| PSR-17 | psr/http-factory |
^1.1 |
ResponseFactoryInterface and StreamFactoryInterface auto-discovered via Psr17FactoryProvider. |
| Routing Engine | nikic/fast-route |
^1.3 |
Regular-expression chunked route compiler wrapped by Dispatcher and FastRouteDispatcher. |
Source Code
from drawlib.canvas import save, setup
from drawlib.smartarts import ChevronProcess, KpiCards
from drawlib.styles import Colors, Styles
setup(width=136, height=48)
proc = ChevronProcess(
style=Styles.Neutral,
text_style=Styles.DarkBold.patch(text_size=10.5),
description_style=Styles.Muted.patch(text_size=9.5),
corner_angle=60.0,
spacing=1.5,
flat_left_end=True,
)
proc.add(
"1. Bootstrap",
description="AppFactory & PSR-17\n(Pages 01–02)",
style=Styles.PrimaryNeutral,
)
proc.add(
"2. Middleware",
description="PSR-15 LIFO Stack\n(Page 04)",
style=Styles.Neutral,
)
proc.add(
"3. Routing",
description="FastRoute & Route\n(Page 03)",
style=Styles.SecondaryNeutral,
)
proc.add(
"4. Invocation",
description="Strategies & DI\n(Page 05)",
style=Styles.PrimaryFlat,
text_style=Styles.WhiteBold.patch(text_size=10.5),
description_style=Styles.White.patch(text_size=9.5),
)
proc.add(
"5. Error / Emit",
description="Renderers & 4xx/5xx\n(Pages 06–07)",
style=Styles.Neutral,
)
proc.draw(xy=(6, 30), width=124, height=13)
white_card = Styles.Neutral.patch(shape_fill_color=Colors.White)
kpis = KpiCards(
style=white_card,
metric_style=Styles.DarkBold.patch(text_size=12.5),
text_style=Styles.DarkBold.patch(text_size=10.2),
description_style=Styles.Muted.patch(text_size=9.5),
accent_style=Styles.PrimaryFlat,
accent_position="top_bar",
metric_layout="top_stack",
spacing=3.0,
)
kpis.add(
"72 PHP Files",
"100% Strict Types",
"55 Classes · 17 Interfaces",
style=Styles.PrimaryNeutral,
metric_style=Styles.PrimaryBold.patch(text_size=12.5),
accent_style=Styles.PrimaryFlat,
)
kpis.add(
"6 PSR-17 Bridges",
"Zero-Config Discovery",
"6 Vendor HTTP Adapters",
accent_style=Styles.SecondaryFlat,
)
kpis.add(
"6 Middlewares",
"Concentric PSR-15",
"Routing · Error · BodyParser",
accent_style=Styles.PrimaryFlat,
)
kpis.add(
"17 Contracts",
"Slim\\Interfaces\\*",
"Segregated Tier Contracts",
accent_style=Styles.AccentFlat,
)
kpis.draw(xy=(6, 4), width=124, height=21, columns=4)
save()
packageSlim\#
Root Application Kernel, Callable Resolution, LIFO Middleware Dispatcher, Response Emitter & Logger — Detailed in 01. App & Factories.
The root Slim\ namespace houses the 5 foundational runtime classes that orchestrate the PSR-15 request handling pipeline, resolve string/container callables, and stream HTTP responses to the SAPI client:
| Symbol | Kind | Implements / Extends | Summary |
|---|---|---|---|
Slim\App |
class |
extends RouteCollectorProxy, implements RequestHandlerInterface |
Primary application entry point (VERSION = '4.15.1'). Registers routes/middleware, executes handle(), strips bodies on HEAD requests (RFC 2616), and emits responses in run(). |
Slim\CallableResolver |
final class |
implements AdvancedCallableResolverInterface |
Resolves closures, invokable objects, [class, method] arrays, and 'Class:method' strings via PSR-11 container lookup or direct instantiation (new $class($container)). |
Slim\MiddlewareDispatcher |
class |
implements MiddlewareDispatcherInterface |
Concentric LIFO (last-in, first-out) PSR-15 middleware stack wrapping the inner RequestHandlerInterface kernel (RouteRunner) with anonymous handler frames. |
Slim\ResponseEmitter |
class |
— | Streams PSR-7 ResponseInterface headers (Set-Cookie multi-header safe), status line, and chunked body (4096 bytes default) to standard output. |
Slim\Logger |
class |
extends Psr\Log\AbstractLogger |
Minimal PSR-3 logger writing stringified messages to PHP's native error_log(). |
packageSlim\Factory#
Static Application & ServerRequest Bootstrap Factories — Detailed in 01. App & Factories.
The Slim\Factory namespace provides zero-boilerplate static factories that wire Slim\App and ServerRequestCreatorInterface instances using either explicit dependencies, a PSR-11 container, or automatic PSR-17 factory discovery:
| Symbol | Kind | Summary |
|---|---|---|
Slim\Factory\AppFactory |
class |
Static builder offering create(), createFromContainer(), determineResponseFactory(), and static setters for pre-configuring container, factories, route collector, and slim/http decoration. |
Slim\Factory\ServerRequestCreatorFactory |
class |
Static builder offering create() and determineServerRequestCreator() to construct a ServerRequestInterface from PHP superglobals ($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES). |
packageSlim\Factory\Psr17#
PSR-17 HTTP Factory Auto-Discovery Registry & Vendor Adapters — Detailed in 02. PSR-17 Providers.
The Slim\Factory\Psr17 namespace contains 10 classes that decouple Slim from any single PSR-7/PSR-17 package by probing installed Composer libraries in priority order (SlimPsr17Factory, HttpSoftPsr17Factory, NyholmPsr17Factory, LaminasDiactorosPsr17Factory, GuzzlePsr17Factory) and optionally wrapping factories with slim/http decorators:
| Symbol | Kind | Implements / Extends | Backed Composer Package |
|---|---|---|---|
Slim\Factory\Psr17\Psr17FactoryProvider |
class |
implements Psr17FactoryProviderInterface |
Registry maintaining the ordered list of Psr17Factory class names (getFactories(), setFactories(), addFactory()). |
Slim\Factory\Psr17\Psr17Factory |
abstract class |
implements Psr17FactoryInterface |
Base adapter checking class_exists() and instantiating ResponseFactoryInterface, StreamFactoryInterface, and ServerRequestCreator. |
Slim\Factory\Psr17\ServerRequestCreator |
class |
implements ServerRequestCreatorInterface |
Invokes a vendor creator object/class and static/instance method via Closure::fromCallable() to build a ServerRequestInterface from globals. |
Slim\Factory\Psr17\SlimHttpServerRequestCreator |
class |
implements ServerRequestCreatorInterface |
Decorator wrapping an inner ServerRequestCreatorInterface with Slim\Http\ServerRequest (slim/http). |
Slim\Factory\Psr17\SlimPsr17Factory |
class |
extends Psr17Factory |
slim/psr7 (Slim\Psr7\Factory\ResponseFactory, StreamFactory, ServerRequestFactory::createFromGlobals). |
Slim\Factory\Psr17\SlimHttpPsr17Factory |
class |
extends Psr17Factory |
slim/http (Slim\Http\Factory\DecoratedResponseFactory wrapping ResponseFactoryInterface + StreamFactoryInterface). |
Slim\Factory\Psr17\NyholmPsr17Factory |
class |
extends Psr17Factory |
nyholm/psr7 & nyholm/psr7-server (Nyholm\Psr7\Factory\Psr17Factory, Nyholm\Psr7Server\ServerRequestCreator::fromGlobals). |
Slim\Factory\Psr17\GuzzlePsr17Factory |
class |
extends Psr17Factory |
guzzlehttp/psr7 (GuzzleHttp\Psr7\HttpFactory, GuzzleHttp\Psr7\ServerRequest::fromGlobals). |
Slim\Factory\Psr17\LaminasDiactorosPsr17Factory |
class |
extends Psr17Factory |
laminas/laminas-diactoros (Laminas\Diactoros\ResponseFactory, StreamFactory, ServerRequestFactory::fromGlobals). |
Slim\Factory\Psr17\HttpSoftPsr17Factory |
class |
extends Psr17Factory |
httpsoft/http-message & httpsoft/http-server-request (HttpSoft\Message\ResponseFactory, StreamFactory, ServerRequestCreator::createFromGlobals). |
packageSlim\Routing#
Route Registration, Groups, URL Generation, FastRoute Dispatch & Execution — Detailed in 03. Routing Subsystem.
The Slim\Routing namespace comprises 11 classes that manage route definitions, nested route groups, URL/path generation (RouteParser), FastRoute regex compilation and caching, request attribute enrichment (RouteContext), and per-route middleware execution:
| Symbol | Kind | Implements / Extends | Summary |
|---|---|---|---|
Slim\Routing\RouteCollectorProxy |
class |
implements RouteCollectorProxyInterface |
Fluent routing DSL (get(), post(), put(), delete(), options(), patch(), any(), map(), group(), redirect()) inherited by Slim\App and passed into group callbacks. |
Slim\Routing\RouteCollector |
class |
implements RouteCollectorInterface |
Central registry of RouteInterface and RouteGroupInterface instances, default invocation strategy, base path, and optional FastRoute cache file. |
Slim\Routing\Route |
class |
implements RouteInterface, RequestHandlerInterface |
Represents a single HTTP route with its own mini MiddlewareDispatcher, route arguments, invocation strategy, and group middleware chain. |
Slim\Routing\RouteGroup |
class |
implements RouteGroupInterface |
Encapsulates a shared URI prefix pattern and group-level middlewares applied to all nested routes. |
Slim\Routing\RouteContext |
final class |
— | Value object extracted from ServerRequestInterface via RouteContext::fromRequest($request) providing typed access to Route, RouteParser, RoutingResults, and basePath. |
Slim\Routing\RouteParser |
class |
implements RouteParserInterface |
Reverse URL generator computing relativeUrlFor(), urlFor(), and fullUrlFor() from named routes, placeholders, and query parameters. |
Slim\Routing\RouteResolver |
class |
implements RouteResolverInterface |
Bridges RouteCollectorInterface and DispatcherInterface to produce RoutingResults and look up matched RouteInterface objects. |
Slim\Routing\RouteRunner |
class |
implements RequestHandlerInterface |
Innermost kernel of Slim\App's middleware stack; ensures routing has run (if RoutingMiddleware was omitted), populates RouteContext::ROUTE, and delegates to Route::run(). |
Slim\Routing\Dispatcher |
class |
implements DispatcherInterface |
Adapter around FastRoute's simpleDispatcher / cachedDispatcher returning RoutingResults and allowed HTTP methods. |
Slim\Routing\FastRouteDispatcher |
class |
extends FastRoute\Dispatcher\GroupCountBased |
Custom FastRoute dispatcher adding transparent HEAD -> GET fallback and deduplicated allowed-method inspection. |
Slim\Routing\RoutingResults |
class |
— | Immutable dispatch outcome holding status (NOT_FOUND = 0, FOUND = 1, METHOD_NOT_ALLOWED = 2), route identifier (routeX), URL-decoded arguments, and allowed methods. |
packageSlim\Middleware#
Built-in PSR-15 Server Middlewares — Detailed in 04. Built-in PSR-15 Middlewares.
The Slim\Middleware namespace provides 6 production-ready PSR-15 (Psr\Http\Server\MiddlewareInterface) implementations for routing, structured exception handling, request body deserialization, HTTP method tunneling, output buffering, and Content-Length calculation:
| Symbol | Kind | Implements | Summary |
|---|---|---|---|
Slim\Middleware\RoutingMiddleware |
class |
MiddlewareInterface |
Executes route matching before downstream middlewares run, attaches route, routeParser, and routingResults request attributes, or throws HttpNotFoundException (404) / HttpMethodNotAllowedException (405). |
Slim\Middleware\ErrorMiddleware |
class |
MiddlewareInterface |
Outermost try / catch (Throwable) boundary that maps exceptions to custom or default ErrorHandlerInterface instances (with optional subclass matching). |
Slim\Middleware\BodyParsingMiddleware |
class |
MiddlewareInterface |
Parses application/json, application/x-www-form-urlencoded, and application/xml (text/xml) request streams into $request->getParsedBody(), with extensible registerBodyParser(). |
Slim\Middleware\ContentLengthMiddleware |
class |
MiddlewareInterface |
Measures $response->getBody()->getSize() after downstream handling and injects the Content-Length header if not already set. |
Slim\Middleware\MethodOverrideMiddleware |
class |
MiddlewareInterface |
Rewrites the HTTP method using X-Http-Method-Override header or _METHOD parsed body parameter on POST requests. |
Slim\Middleware\OutputBufferingMiddleware |
class |
MiddlewareInterface |
Captures stray echo / print output via ob_start() and either APPENDs or PREPENDs it to the PSR-7 response stream. |
packageSlim\Handlers#
Default ErrorHandler & Route Invocation Strategies — Detailed in 05. Error Handler & Route Strategies.
The Slim\Handlers and Slim\Handlers\Strategies namespaces contain the content-negotiating ErrorHandler and the 4 built-in route callback invocation strategies (InvocationStrategyInterface):
| Symbol | Kind | Implements | Callback Signature Dispatched |
|---|---|---|---|
Slim\Handlers\ErrorHandler |
class |
ErrorHandlerInterface |
Negotiates Accept content type (json, xml, html, text/plain), logs unhandled errors via PSR-3, adds Allow headers on 405, and builds the error ResponseInterface. |
Slim\Handlers\Strategies\RequestResponse |
class |
InvocationStrategyInterface |
Default strategy: function (ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface. |
Slim\Handlers\Strategies\RequestResponseArgs |
class |
InvocationStrategyInterface |
Unpacks route parameters positionally: function (ServerRequestInterface $request, ResponseInterface $response, string $arg1, ...): ResponseInterface. |
Slim\Handlers\Strategies\RequestResponseNamedArgs |
class |
InvocationStrategyInterface |
Passes route parameters as PHP 8+ named arguments: function (ServerRequestInterface $request, ResponseInterface $response, string $id, ...): ResponseInterface. |
Slim\Handlers\Strategies\RequestHandler |
class |
RequestHandlerInvocationStrategyInterface |
PSR-15 style route handler (function (ServerRequestInterface $request): ResponseInterface), optionally injecting route args as request attributes. |
packageSlim\Error\Renderers#
Content-Negotiated Error Payload Renderers — Detailed in 06. Error Renderers.
The Slim\Error and Slim\Error\Renderers namespaces format Throwable exceptions into string payloads (application/json, text/html, application/xml, text/plain) honoring the $displayErrorDetails flag and chained $exception->getPrevious() causes:
| Symbol | Kind | Implements / Extends | MIME Types Served (ErrorHandler) |
|---|---|---|---|
Slim\Error\AbstractErrorRenderer |
abstract class |
implements ErrorRendererInterface |
Base class resolving HttpException titles/descriptions vs generic 500 defaults ($defaultErrorTitle, $defaultErrorDescription). |
Slim\Error\Renderers\JsonErrorRenderer |
class |
extends AbstractErrorRenderer |
application/json, application/vnd.api+json (JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES). |
Slim\Error\Renderers\HtmlErrorRenderer |
class |
extends AbstractErrorRenderer |
text/html, application/xhtml+xml (with htmlentities() escaping). |
Slim\Error\Renderers\XmlErrorRenderer |
class |
extends AbstractErrorRenderer |
application/xml, text/xml (<![CDATA[...]]> wrapped XML error document). |
Slim\Error\Renderers\PlainTextErrorRenderer |
class |
extends AbstractErrorRenderer |
text/plain (and default log renderer for ErrorHandler). |
packageSlim\Exception#
Typed HTTP 4xx & 5xx Exception Hierarchy — Detailed in 07. HTTP Exceptions.
The Slim\Exception namespace defines 11 exception classes rooted at RuntimeException that carry structured HTTP status codes, titles, descriptions, and the triggering ServerRequestInterface:
| Symbol | Status Code | Default Message | Parent Class |
|---|---|---|---|
Slim\Exception\HttpException |
500 |
'' |
RuntimeException |
Slim\Exception\HttpSpecializedException |
(abstract) | (inherited) | HttpException |
Slim\Exception\HttpBadRequestException |
400 |
'Bad request.' |
HttpSpecializedException |
Slim\Exception\HttpUnauthorizedException |
401 |
'Unauthorized.' |
HttpSpecializedException |
Slim\Exception\HttpForbiddenException |
403 |
'Forbidden.' |
HttpSpecializedException |
Slim\Exception\HttpNotFoundException |
404 |
'Not found.' |
HttpSpecializedException |
Slim\Exception\HttpMethodNotAllowedException |
405 |
'Method not allowed.' |
HttpSpecializedException |
Slim\Exception\HttpGoneException |
410 |
'Gone.' |
HttpSpecializedException |
Slim\Exception\HttpTooManyRequestsException |
429 |
'Too many requests.' |
HttpSpecializedException |
Slim\Exception\HttpInternalServerErrorException |
500 |
'Internal server error.' |
HttpSpecializedException |
Slim\Exception\HttpNotImplementedException |
501 |
'Not implemented.' |
HttpSpecializedException |
packageSlim\Interfaces#
All 17 Architectural Contract Interfaces — Detailed in 08. Contract Interfaces.
Every subsystem in Slim 4 depends on interfaces under Slim\Interfaces\* rather than concrete classes, enabling drop-in replacement via AppFactory or a PSR-11 container: