Slim Framework Version 2.3.0

The Slim Framework contains many new and helpful features in version 2.3.0. It is backwards compatible with the previous version. While I update the official documentation, here are some tips to help you get started with Slim’s new features.

Download Version 2.3.0

Route Groups

It is now possible to group routes. This helps you avoid duplicating route URL prefixes in each route definition (e.g. “/api/users”).

 * API route group
$app->group('/api', function () use ($app) {
     * User route group
    $app->group('/users', function () use ($app) {
         * New user --> GET /api/users/new
        $app->get('/new', function () use ($app) {

     * Role route group
    $app->group('/roles', function () use ($app) {
         * New role --> GET /api/roles/new
        $app->get('/new', function () use ($app) {

Resource Locator

Slim now provides a resource locator. This allows you to easily inject objects into a Slim application or to quickly modify internal components of a Slim application (e.g. the request object).

You can use the Resource Locator to inject any resource into a Slim application.

// Set value
$app->foo = 'bar';

// Get value
$value = $app->foo;
You can also inject a resource as a closure that will be invoked whenever the given resource is requested.

// Set value
$app->now = function () {
    return time();

// Get value
$now = $app->now;

If you inject a resource as a closure, it will be invoked each time the resource is requested. If you need the resource to be computed only once and then remain the same each time it is requested (i.e. a singleton), you’ll need to do this:

// Set singleton value
$app->container->singleton('db', function () {
    return new PDO('sqlite:database.db');

// Get singleton value
$pdo = $app->db;

Many of Slim’s internal objects (e.g. Environment, Request, Response, View, Log) are injected using the singleton method demonstrated above. You can easily override Slim’s default implementation for any of these objects.

$app = new \Slim\Slim();

// Override Slim's default Response object
$app->container->singleton('response', function () {
    return new \My\Response();

Slim will now use your custom \My\Response class for its internal Response object. Slim currently does not codify expected interfaces for its internal objects; if you intend to override any of Slim’s internal objects, I recommend you extend the internal objects with a subclass. The next point release will codify Slim’s expectations with interfaces allowing you more flexibility when overriding Slim’s default internal objects.


Because Slim uses a Resource Locator, it is now possible to directly access internal application objects (e.g. Request and Response) as public properties on the Slim application instance. Previously you relied on accessor methods like request() and response(). Now you can simply do:

$app = new \Slim\Slim();
$app->get('/', function () use ($app) {
    // Get environment
    $env = $app->environment;

    // Get request
    $req = $app->request;

    // Get response
    $res = $app->response;

    // Get view
    $view = $app->view;

    // Get log
    $log = $app->log;

Set Interface

Version 2.3.0 includes a new \Slim\Helper\Set interface. This interface simplifies and standardizes how you interact with Slim application collections, such as cookies and headers on the Request and Response objects. The interface is:

set($key, mixed $value);
get($key, mixed $defaultValue = null);
replace(array $items);

The \Slim\Helper\Set class also implements the ArrayAccess, Countable, and IteratorAggregate interfaces.


Both the Request and Response objects contain a public headers property. This property is an instance of \Slim\Helper\Set. This interface allows you to easily fetch a Request object’s headers like this:

$headers = $app->request->headers->all();
$header = $app->request->headers->get('Content-Type');

You can just as easily manipulate headers on the Response object like this:

$app->response->headers->set('X-Foo', 'Bar');


Both the Request and Response objects contain a public cookies property. This property is an instance of \Slim\Helper\Set. This interface allows you to easily fetch a Request object’s cookies like this:

$cookies = $app->request->cookies->all();
$cookie = $app->request->cookies->get('foo');

You can just as easily manipulate cookies on the Response object like this:

$app->response->cookies->set('foo', 'bar');

When you set a cookie on the Response object, you can also pass an array as the cookie value to define additional cookie parameters:

$app->response->cookies->set('foo', array(
    'value' => 'bar',
    'domain' => '',
    'path' => '/',
    'expires' => time() + 3600,
    'secure' => true,
    'httponly' => true

Because Slim now uses the \Slim\Helper\Set interface to manage Response cookies, the cookie values are not serialized and encrypted until the very end of the Slim application lifecycle. This means you may continue manipulating cookie values using the new \Slim\Helper\Set interface even after the cookies are originally set.

Keep in mind, this new interface applies only to the Response object implementation. You may continue using the \Slim\Slim::setCookie() and \Slim\Slim::getCookie() methods just like before.

Slim Extras

The current Slim-Extras repository is undergoing major changes. We will be drastically simplifying first-party custom views to just Twig and Smarty. Other third party views will be deprecated.

We will also be re-organizing the Slim-Extras repository into more specific repositories. Custom views will be in the Slim-Views repository. Both the Twig and Smarty custom views have received major (potentially breaking) changes. Be sure you read their respective README files for updates.

We will also be building out a new section on the Slim Framework website to help you explore and find third-party Slim Framework addons (e.g. middleware, views, loggers). This new addition will be coming soon. Stay tuned for updates.

Full Changelog

  • Added route groups
  • Added resource locator
  • Added HTTP PATCH method support
  • Added new \Slim\Helper\Set interface
  • Fixed XSS exploit in \Slim\Slim::urlFor method
  • Simplified default error handler \Slim\Slim::handleErrors
  • Removed \Slim\Middleware\PrettyExceptions when not in debug mode
  • Let HTTP headers retain HTTP_ prefix if present
  • Added \Slim\Helper\Set interface to \Slim\Http\Headers and \Slim\Http\Cookies
  • Updated \Slim\Slim so that environment, request, response, router, log, and view are public properties.
  • Updated \Slim\Http\Request and \Slim\Http\Response so that headers and cookies are public properties that implement \Slim\Helper\Set.
  • Added \Slim\Http\Response methods setStatus(), getStatus(), setBody(), getBody(), and getLength()
  • Updated \Slim\Http\Response object so that its cookies and headers are not serialized and encrypted until the very end of the app lifecycle.
  • Updated \Slim\Log with PSR-3 interface
  • Deprecated \Slim\Log::warn(), replaced with \Slim\Log::warning()
  • Deprecated \Slim\Log::fatal(), replaced with \Slim\Log::critical()
  • Deprecated \Slim\Http\Request methods cookies() and headers()
  • Deprecated \Slim\Http\Response methods headers(), header(), length(), body(), status()
  • Deprecated \Slim\Http\Response interfaces ArrayAccess, Countable, and IteratorAggregate