Skip to content

Slim 2 Application Lifecycle

Rob Allen edited this page Oct 2, 2018 · 1 revision

Application initialization

A Slim application is initialized when you call Slim::init(). This method instantiates the Slim app, creates the Request object, creates the Response object, creates the Router object, defines default settings, and sets default Error and Not Found handlers.

Request Object

The Request object intercepts the HTTP request and parses information needed by the Slim application. This information is:

  • The request method (GET, POST, PUT, DELETE)
  • The resource URI
  • GET, POST, PUT, COOKIE parameters (strips slashes if magic quotes is enabled)
  • Is the Request made via AJAX?
  • Checks for manual method override

Response Object

The Response object provides a default object that is modified throughout the lifetime of the Slim application. The default Response object assumes a 200 OK response of type text/html with an empty body.

Router Object

The Router object acts as a controller for your Slim application's routes (see Slim::get, Slim::post, Slim::put, Slim::delete). The Router class implements the Iterable interface, so you can quickly iterate over routes that match the current request.

Default Settings

See "Application Settings" in the Documentation.

Default Error and Not Found Handlers

The default Error handler will display a message and stack trace for any Exceptions or Errors in your application; the Error handler will return a 500 response code.

The default Not Found handler will display a brief message and return a 404 response code.

Running Your Application

After you have initialized your application and added routes, you will call Slim::run(). This executes your Slim application.

  1. Slim first activates output buffering.
  2. Next, Slim will iterate through each route that matches the current request in the order the routes were specified. The first matching route to successfully dispatch will determine the response returned to the client; "dispatch" means that the callable object attached to the given route will be invoked and potentially provided a set of arguments.
  3. The output echo'd by the invoked callable will be captured by the output buffer. If the invoked callable chooses to pass (see Slim::pass), the output buffer is cleared and the subsequent matching route is invoked.
  4. After a route is successfully invoked (or the Not Found handler is invoked if no matching routes are available), the output buffer content is written to the Response body.
  5. Finally, the Response is returned to the browser. The Response object is responsible for setting headers, cookies, and returning the body content to the client.

Plugin Hooks

(only available in the DEVELOP branch until version 1.1 is officially released)

The available hooks within a Slim application are:

slim.before

This hook is invoked before the Slim application is run and before output buffering is turned on. This hook is invoked once during the Slim application lifecycle.

slim.before.router

This hook is invoked after output buffering is turned on and before the router is dispatched. This hook is invoked once during the Slim application lifecycle.

slim.before.dispatch

This hook is invoked before the current matching route is dispatched. Usually this hook is invoked only once during the Slim application lifecycle; however, this hook may be invoked multiple times if a matching route chooses to pass to a subsequent matching route.

slim.after.dispatch

This hook is invoked after the current matching route is dispatched. Usually this hook is invoked only once during the Slim application lifecycle; however, this hook may be invoked multiple times if a matching route chooses to pass to a subsequent matching route.

slim.after.router

This hook is invoked after the router is dispatched, before the Response is sent to the client, and before output buffering is turned off. This hook is invoked once during the Slim application lifecycle.

slim.after

This hook is invoked after output buffering is turned off and after the Response is sent to the client. This hook is invoked once during the Slim application lifecycle.