addSubscriber(new TemplateRenderingListener($app)); /** * Todo: * * Custom Controller Resolver as per http://davedevelopment.co.uk/2012/10/03/Silex-Controllers-As-Services.html * * Setup Twig * * Setup Doctrine ORM */ $app['findOr404Factory'] = $app->protect(function($type, $message = null) use ($app) { if (null === $message) { $lastNamespace = strrpos($type, "\\"); if ($lastNamespace !== false) { $message = substr($type, strrpos($type, "\\") + 1) . " Not Found"; } else { $message = "$type Not Found"; } } return function($id) use ($app, $type, $message) { $obj = $app['em']->getRepository($type)->find($id); if (null === $obj) { return $app->abort(404, $message); } return $obj; }; }); /** * Routes */ $app->get("/posts/{post}", "posts.controller:viewAction") ->convert("post", $app['findOr404Factory']("Demo\Entity\Post")) ->template("post/edit.html.twig");