_posts/2013-07-14-version-230.md
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.
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) {
$app->render('new_user.html');
});
});
/**
* Role route group
*/
$app->group('/roles', function () use ($app) {
/**
* New role --> GET /api/roles/new
*/
$app->get('/new', function () use ($app) {
$app->render('new_role.html');
});
});
});
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;
});
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);
all();
keys();
has($key);
remove($key);
clear();
count();
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' => 'example.com',
'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.
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.
\Slim\Slim::urlFor method\Slim\Slim::handleErrorssetStatus(), getStatus(), setBody(), getBody(), and getLength()\Slim\Log::warn(), replaced with \Slim\Log::warning()\Slim\Log::fatal(), replaced with \Slim\Log::critical()cookies() and headers()headers(), header(), length(), body(), status()