Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs. At its core, Slim is a dispatcher that receives an HTTP request, invokes an appropriate callback routine, and returns an HTTP response..1
Table of contents
|Slim framework tutorial part 1: installation||A Slim 3 primer - Rob Allen|
|Intro to Slim Framework|
- Slim Framework discussion forum
- Create REST applications with the Slim micro-framework - Slim is a full-featured, open-source micro-framework for PHP application development. It comes with a sophisticated URL router and middleware architecture that makes it ideal for static websites or API prototyping. This article examines Slim in detail, illustrating how you can use it to rapidly build and deploy a REST API with support for authentication and multiple request/response formats.
- First Application Walkthrough - If you’re looking for a tour through all the ingredients for setting up a very simple Slim application (this one doesn’t use Twig, but does use Monolog and a PDO database connection) then you’re in the right place. Either walk through the tutorial to build the example application, or adapt each step for your own needs.
- A Slim 3 primer Frameworks, May 2015
- How To Create A Simple REST API in PHP? Step By Step Guide!
- How to build a RESTful API in Slim 3 - Part 1: Application Setup
Model-View-Controller - The Front Controller consolidates all request handling by channeling requests through a single handler object. This object can carry out common behavior, which can be modified at runtime with decorators. The handler then dispatches to command objects for behavior particular to a request.2
Communication between a host and a client occurs, via a request/response pair. The client initiates an HTTP request message, which is serviced through a HTTP response message in return. 3 See Hypertext Transfer Protocol
Slim supports PSR-7 interfaces for its Request and Response objects. This makes Slim flexible because it can use any PSR-7 implementation. For example, a Slim application route does not have to return an instance of \Slim\Http\Response. It could, for example, return an instance of \GuzzleHttp\Psr7\CachingStream or any instance returned by the \GuzzleHttp\Psr7\stream_for() function.
Slim provides its own PSR-7 implementation so that it works out of the box. However, you are free to replace Slim’s default PSR 7 objects with a third-party implementation. Just override the application container’s request and response services so they return an instance of \Psr\Http\Message\ServerRequestInterface and \Psr\Http\Message\ResponseInterface, respectively.4
This document describes common interfaces for representing HTTP messages as described in RFC 7230 and RFC 7231, and URIs for use with HTTP messages as described in RFC 3986. See PSR-7: HTTP message interfaces
You can run code before and after your Slim application to manipulate the Request and Response objects as you see fit. This is called middleware. Why would you want to do this? Perhaps you want to protect your app from cross-site request forgery. Maybe you want to authenticate requests before your app runs. Middleware is perfect for these scenarios.5
Slim uses a dependency container to prepare, manage, and inject application dependencies. Slim supports containers that implement the Container-Interop interface. You can use Slim’s built-in container (based on Pimple) or third-party containers like Acclimate or PHP-DI.6
Dependency Injection - In software engineering, dependency injection is a technique whereby one object supplies the dependencies of another object. A dependency is an object that can be used (a service). An injection is the passing of a dependency to a dependent object (a client) that would use it. The service is made part of the client's state. Passing the service to the client, rather than allowing a client to build or find the service, is the fundamental requirement of the pattern.7