Created
November 26, 2012 23:45
-
-
Save davedevelopment/4151421 to your computer and use it in GitHub Desktop.
Revisions
-
davedevelopment revised this gist
Nov 26, 2012 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,11 +3,11 @@ namespace Demo\Controller; use Demo\Entity\User; class PostController { public function viewAction(Post $post) { /** Do something clever */ return array("post" => $post); -
davedevelopment created this gist
Nov 26, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,48 @@ <?php use Silex\Application; use Demo\Entity\Post; use Demo\Controller\PostController; $app = new Application; $app['route_class'] = 'CustomRoute'; $app['dispatcher']->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"); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ <?php namespace Demo\Controller; use Demo\Entity\User; use Symfony\Component\HttpFoundation\Request; class PostController { public function viewAction(Post $post, Request $request) { /** Do something clever */ return array("post" => $post); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,14 @@ <?php namespace Demo; use Silex\Route; class CustomRoute extends Route { public function template($path) { $this->setOption('_template', $path); return $this; } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,46 @@ <?php namespace Demo\EventListener; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\Response; class TemplateRenderingListener implements EventSubscriberInterface { protected $app; public function __construct(Application $app) { $this->app = $app; } public function onKernelView(GetResponseForControllerResultEvent $event) { $response = $event->getControllerResult(); if (!is_array($response)) { return; } $request = $event->getRequest(); $routeName = $request->attributes->get('_route'); if (!$route = $this->app['routes']->get($routeName)) { return; } if (!$template = $route->getOption('_template')) { return; } $output = $this->app['twig']->render($template, $response); $event->setResponse(new Response($output)); } public static function getSubscribedEvents() { return array( KernelEvents::VIEW => array('onKernelView', -10), ); } }