Slim 4 Documentation

This documentation is for Slim 4. Looking for Slim 3 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:

  1. <?php
  2. use Psr\Http\Message\ResponseInterface as Response;
  3. use Psr\Http\Message\ServerRequestInterface as Request;
  4. use Slim\Factory\AppFactory;
  5. require __DIR__ . '/../vendor/autoload.php';
  6. /**
  7. * Instantiate App
  8. *
  9. * In order for the factory to work you need to ensure you have installed
  10. * a supported PSR-7 implementation of your choice e.g.: Slim PSR-7 and a supported
  11. * ServerRequest creator (included with Slim PSR-7)
  12. */
  13. $app = AppFactory::create();
  14. // Add Routing Middleware
  15. $app->addRoutingMiddleware();
  16. /*
  17. * Add Error Handling Middleware
  18. *
  19. * @param bool $displayErrorDetails -> Should be set to false in production
  20. * @param bool $logErrors -> Parameter is passed to the default ErrorHandler
  21. * @param bool $logErrorDetails -> Display error details in error log
  22. * which can be replaced by a callable of your choice.
  23. * Note: This middleware should be added last. It will not handle any exceptions/errors
  24. * for middleware added after it.
  25. */
  26. $errorMiddleware = $app->addErrorMiddleware(true, true, true);
  27. // Define app routes
  28. $app->get('/hello/{name}', function (Request $request, Response $response, $args) {
  29. $name = $args['name'];
  30. $response->getBody()->write("Hello, $name");
  31. return $response;
  32. });
  33. // Run app
  34. $app->run();
Figure 1: Example Slim application

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. Creative Commons License