Slim 3 Documentation
This documentation is for Slim 3. Looking for Slim 2 Docs?.
Welcome
Slim is a PHP micro framework that helps youquickly write simple yet powerful web applications and APIs. At its core, Slimis a dispatcher that receives an HTTP request, invokes an appropriate callbackroutine, and returns an HTTP response. That’s it.
What’s the point?
Slim is an ideal tool to create APIs that consume, repurpose, or publish data. Slim is alsoa great tool for rapid prototyping. Heck, you can even build full-featured webapplications with user interfaces. More importantly, Slim is super fastand has very little code. In fact, you can read and understand its source codein only an afternoon!
At its core, Slimis a dispatcher that receives an HTTP request, invokes an appropriate callbackroutine, and returns an HTTP response. That’s it.
You don’t always need a kitchen-sink solution like Symfony or Laravel.These are great tools, for sure. But they are often overkill. Instead, Slimprovides only a minimal set of tools that do what you need and nothing else.
How does it work?
First, you need a web server like Nginx or Apache. You should configureyour web server so that it sends all appropriaterequests to one “front-controller” PHP file. You instantiate and run your Slimapp in this PHP file.
A Slim app contains routes that respond to specific HTTP requests. Each routeinvokes a callback and returns an HTTP response. To get started, you firstinstantiate and configure the Slim application. Next, you define your applicationroutes. Finally, you run the Slim application. It’s that easy. Here’s anexample application:
<?php
// Create and configure Slim app
$config = ['settings' => [
'addContentLengthHeader' => false,
]];
$app = new \Slim\App($config);
// Define app routes
$app->get('/hello/{name}', function ($request, $response, $args) {
return $response->write("Hello " . $args['name']);
});
// Run app
$app->run();
Request and response
When you build a Slim app, you are often working directly with Requestand Response objects. These objects represent the actual HTTP request receivedby the web server and the eventual HTTP response returned to the client.
Every Slim app route is given the current Request and Response objects as argumentsto its callback routine. These objects implement the popular PSR-7 interfaces. The Slim app route can inspector manipulate these objects as necessary. Ultimately, each Slim app routeMUST return a PSR-7 Response object.
Bring your own components
Slim is designed to play well with other PHP components, too. You can registeradditional first-party components such as Slim-Csrf, Slim-HttpCache,or Slim-Flash that build upon Slim’s default functionality. It’s alsoeasy to integrate third-party components found on Packagist.
How to read this documentation
If you are new to Slim, I recommend you read this documentation from startto finish. If you are already familiar with Slim, you can instead jump straightto the appropriate section.
This documentation begins by explaining Slim’s concepts and architecturebefore venturing into specific topics like request and response handling,routing, and error handling.
Documentation License
This website and documentation is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.