Upgrading to Slim 2+
So with the new Slim comes new hurdles for existing users and first time users. Upgrading your existing app is easier than you might think, with all the new PHP 5.3 namespacing users often get lost on how to load their classes. Here I will show a few examples of what to do to easily upgrade your existing Slim app.
Your current app might look like this:
require 'Slim/Slim.php';
$app = new Slim();
$app->get('/', function () use ($app) {
$app->render('hello.html');
});
Moving this over to Slim 2+ requires you making no change on how
you instantiate your app at all, all you would need to do is add in
two lines before the Slim class is instantiated and use the PHP 5.3
use
statement.
require 'Slim/Slim.php';
use Slim\Slim;
Slim::registerAutoloader();
$app = new Slim();
$app->get('/', function () use ($app) {
$app->render('hello.html');
});
One more thing to note is that if you are using a non namespaced
class inside a namespaced Slim class i.e Middleware, you will need
to add a \
before your class name where it is
instantiated.
Middleware
If you are using Slim middleware in your current app, here is how you would transition to the new Slim 2+ way of using them.
require 'Slim/Slim.php';
require 'Extras/Middleware/HttpBasicAuth.php';
use Slim\Slim;
use Slim\Extras\Middleware\HttpBasicAuth;
Slim::registerAutoloader();
$app = new Slim();
$app->add(new HttpBasicAuth('test', 'password'));
$app->get('/', function () use ($app) {
$app->render('hello.html');
});
Notice I am using the use
statement again, this
will save you making many changes to your code.
For those of you using Composer, you won't need to require all those file but rather use the Composer autoloader instead.