I recently released Slim Framework 2. This is a radical departure from past versions. Version 2 introduces PSR-2 compliance and PHP namespaces. It requires PHP 5.3 or newer.
The PHP language and its community are in metamorphosis. The PHP Framework Interop Group has introduced recommended coding styles that encourage component libraries instead of monolithic frameworks. Like other PHP libraries, Slim is adopting the strictest of these standards. This change makes it simple for other developers to read Slim’s codebase and contribute improvements.
The Slim Framework also uses PHP namespaces. Namespaces remove the need for long, PEAR-style class names (e.g. Slim_Middleware_ContentTypes) while still avoiding class name collisions with other libraries. This is a good thing. Version 2 uses the “Slim” vendor namespace. For example, to add the ContentTypes middleware to a Slim Framework application:
Namespaces begin with the vendor namespace (“\Slim”) and end with the namespace path to the desired class name. PHP namespaces may be compared to an imaginary directory structure for PHP classes.
Version 2 introduces PSR-2 compliance and PHP namespaces. These provide a new foundation for future development.
How To Install Version 2
Install with Composer
Composer is the recommended way to install the Slim Framework. Composer is a package manager for PHP (similar to Node’s npm). Composer makes it super easy to declare and autoload your project’s dependencies without worrying about writing or registering your own autoloaders. Trust me, Composer really is amazing, and it’s worth your time to learn it. It has forever changed the way I develop PHP applications (for the better).
For example, assume you want to create a new PHP application using Slim 2 and Twig. First, install Composer in your project root directory with this bash command:
Next, create a
composer.json file that declares your project’s dependencies (Slim and Twig):
Next, install the project dependencies with Composer using this bash command. This will download the dependencies declared in
composer.json into a
Finally, add this line to your application’s index.php file to have Composer automatically autoload the project’s dependencies:
You may still manually install the Slim Framework, too:
- Download and extract the Slim Framwork ZIP archive.
- Copy the
Slim/directory into your project.
- Require the Slim class in your application’s
- Register Slim’s built-in autoloader with
Here’s an example
index.php file that uses a manual install:
HOw To Upgrade Older Applications
Upgrade the Slim Framework
Follow the Manual Install instructions above and replace the older Slim Framework library with version 2. Make sure you register Slim’s built-in autoloader, too.
Replace all Slim Framework class names with their namespaced equivalents. For example, when you instantiate a Slim Framework application, replace this:
If you add middleware, do the same. For example, if you use the Slim_Middleware_ContentTypes middleware, replace it with the namespaced version like this:
You’ll need to make this change everywhere you reference a Slim-specific class name in your code.
I understand this is a large change. However, I believe this change guarantees a better, brighter future for the Slim Framework. If you have any trouble with version 2, please ask questions on the Slim Framework support forum. I and many other Slim Framework users are ready and willing to help answer your questions.