First, you instantiate the
Slim\App class. This is the Slim application object. During instantiation, Slim registers default services for each application dependency.
Second, you define routes using the application instance’s
options() routing methods. These instance methods register a route with the application’s Router object. Each routing method returns the Route instance so you can immediately invoke the Route instance’s methods to add middleware or assign a name.
Third, you invoke the application instance’s
run() method. This method starts the following process:
run() method begins to inwardly traverse the application’s middleware stack. This is a concentric structure of middleware layers that receive (and optionally manipulate) the Environment, Request, and Response objects before (and after) the Slim application runs. The Slim application is the inner-most layer of the concentric middleware structure. Each middleware layer is invoked inwardly beginning from the outer-most layer.
run() method reaches the inner-most middleware layer, it invokes the application instance and dispatches the current HTTP request to the appropriate application route object. If a route matches the HTTP method and URI, the route’s middleware and callable are invoked. If a matching route is not found, the Not Found or Not Allowed handler is invoked.
After the application dispatch process completes, each middleware layer reclaims control outwardly, beginning from the inner-most layer.
After the outer-most middleware layer cedes control, the application instance prepares, serializes, and returns the HTTP response. The HTTP response headers are set with PHP’s native
header() method, and the HTTP response body is output to the current output buffer.