Pragmatism in the real world

Redirecting in Slim 2 Middleware

I recently ran into a problem with calling redirect() in a Slim Framework 2 middleware class, so I thought I'd better document the solution so that I remember in future! The authentication middleware looks roughly like this: class Authentication extends \Slim\Middleware { public function call() { if (!$this->isLoggedIn()) { $app = \Slim\Slim::getInstance(); $app->redirect($app->urlFor('login')); } $this->next->call(); } // other methods… } The problem is that you can't call $app->redirect() in Middleware as redirect() is designed to… continue reading.

Integrating ZF2 forms into Slim

Let's say that you want to use Zend Framework 2's Form component outside of ZF2 itself. In this case, a Slim application. It turns out that Composer makes this quite easy, though there's quite a lot of code involved, so this is a long article. Start with a really simple Slim Application. index.php: require 'vendor/autoload.php'; $app = new \Slim\Slim(); $app->map('/', function () use ($app) { $app->render('home.php', array( 'form' => $form )); })->via('GET', 'POST'); $app->run(); templates/home.php:… continue reading.