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”).
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.
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:
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.
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:
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:
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:
You can just as easily manipulate headers on the Response object like this:
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:
You can just as easily manipulate cookies on the Response object like this:
When you set a cookie on the Response object, you can also pass an array as the cookie value to define additional cookie parameters:
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::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.
- Added route groups
- Added resource locator
- Added HTTP PATCH method support
- Added new \Slim\Helper\Set interface
- Fixed XSS exploit in
- Simplified default error handler
- 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
- 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
\Slim\Log::warn(), replaced with
\Slim\Log::fatal(), replaced with
- Deprecated \Slim\Http\Request methods
- Deprecated \Slim\Http\Response methods
- Deprecated \Slim\Http\Response interfaces ArrayAccess, Countable, and IteratorAggregate