Your SlideShare is downloading. ×
0
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Silex - The Symfony2 Microframework
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Silex - The Symfony2 Microframework

22,227

Published on

Published in: Technology
1 Comment
43 Likes
Statistics
Notes
  • hi! Excusme into the slide 22 you write an example with PDO, but in my script don't found https://github.com/GianArb/sermig-app-facebook/blob/master/bootstrap.php#L17

    my route localhost/ return two notice Notice: Undefined variable: dbh in /Users/gianarb92/Sites/sermig-app/bootstrap.php on line 17 Notice: Undefined variable: dbh in /Users/gianarb92/Sites/sermig-app/web/index.php on line 9
    Can you help me please?
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
22,227
On Slideshare
0
From Embeds
0
Number of Embeds
57
Actions
Shares
0
Downloads
222
Comments
1
Likes
43
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. μWednesday, March 2, 2011
  • 2. Who? • phpBB developer • GH: igorwWednesday, March 2, 2011 • Twitter: @igorwesome
  • 3. MicroframeworksWednesday, March 2, 2011
  • 4. What? • Bare bones • Routes mapped to actions • The ‘C’ of ‘MVC’ • REST • Single file appWednesday, March 2, 2011
  • 5. Why? • Frameworks are: • heavy • verbose • complexWednesday, March 2, 2011
  • 6. Sometimes a full-stack framework is too much for a simple task.Wednesday, March 2, 2011
  • 7. Wednesday, March 2, 2011
  • 8. require sinatra get / do Hello world! endWednesday, March 2, 2011
  • 9. Ian Press PhotographyWednesday, March 2, 2011
  • 10. SilexWednesday, March 2, 2011
  • 11. Silex (by fabpot)Wednesday, March 2, 2011
  • 12. require_once __DIR__./silex.phar; use SilexFramework; $app = new Framework(); $app->get(/, function() { return "Hello world!"; });Wednesday, March 2, 2011
  • 13. require_once __DIR__./silex.phar; use SilexFramework; $app = new Framework(); $app->get(/, function() { Phar return "Hello world!"; });Wednesday, March 2, 2011
  • 14. require_once __DIR__./silex.phar; use SilexFramework; $app = new Framework(); Instance $app->get(/, function() { return "Hello world!"; });Wednesday, March 2, 2011
  • 15. require_once __DIR__./silex.phar; use SilexFramework; $app = new Framework(); $app->get(/, function() { return "Hello world!"; Closure });Wednesday, March 2, 2011
  • 16. $app->run();Wednesday, March 2, 2011
  • 17. <IfModule mod_rewrite.c> RewriteEngine On RewriteBase /some/path RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [QSA,L] </IfModule>Wednesday, March 2, 2011
  • 18. Dynamic RoutingWednesday, March 2, 2011
  • 19. $app->get(/hello/{name}, function($name) { return "Hello $name!"; });Wednesday, March 2, 2011
  • 20. Before & AfterWednesday, March 2, 2011
  • 21. $app->before(function() use ($conn) { $conn = new PDO(mysql:dbname=myapp); }); $app->get(/, function() use ($conn) { $usernames = array(); $sql = SELECT * FROM user ORDER BY email; foreach ($conn->query($sql) as $row) { $usernames[] = $row[username]; } return implode(<br />, $usernames); }); $app->after(function() use ($conn) { $conn = null; });Wednesday, March 2, 2011
  • 22. $app->before(function() use ($conn) { $conn = new PDO(mysql:dbname=myapp); }); $app->get(/, function() use ($conn) { $usernames = array(); $sql = SELECT * FROM user ORDER BY email; foreach ($conn->query($sql) as $row) { $usernames[] = $row[username]; } return implode(<br />, $usernames); }); $app->after(function() use ($conn) { $conn = null; });Wednesday, March 2, 2011
  • 23. $app->before(function() use ($conn) { $conn = new PDO(mysql:dbname=myapp); }); $app->get(/, function() use ($conn) { $usernames = array(); $sql = SELECT * FROM user ORDER BY email; foreach ($conn->query($sql) as $row) { $usernames[] = $row[username]; } return implode(<br />, $usernames); }); $app->after(function() use ($conn) { $conn = null; });Wednesday, March 2, 2011
  • 24. $app->before(function() use ($conn) { $conn = new PDO(mysql:dbname=myapp); }); $app->get(/, function() use ($conn) { $usernames = array(); $sql = SELECT * FROM user ORDER BY email; foreach ($conn->query($sql) as $row) { $usernames[] = $row[username]; } return implode(<br />, $usernames); }); $app->after(function() use ($conn) { $conn = null; });Wednesday, March 2, 2011
  • 25. RESTWednesday, March 2, 2011
  • 26. use SymfonyComponentHttpFoundationResponse; $app->post(/blog/post/{id}, function($id) use ($app) { $request = $app->getRequest(); $id = (int) $id; $filename = __DIR__."/data/$id.json"; if (file_exists($filename)) { return new Response(, 409); } file_put_contents($filename, json_encode(array( id => $id, title => $request->get(title), body => $request->get(body), ))); return new Response(, 201); });Wednesday, March 2, 2011
  • 27. $app->delete(/blog/post/{id}, function($id) { $id = (int) $id; $filename = __DIR__."/data/$id.json"; if (!file_exists($filename)) { return new Response(, 404); } unlink($filename); return new Response(, 200); });Wednesday, March 2, 2011
  • 28. Errors (Exceptions)Wednesday, March 2, 2011
  • 29. use MonologLogger; use MonologHandlerFileHandler; use SymfonyComponentHttpKernelExceptionNotFoundHttpException; $log = new Logger(myapp); $log->pushHandler(new FileHandler(__DIR__.log/error.log, Logger::ERROR)); $app->error(function(Exception $e) use ($log) { if ($e instanceof NotFoundHttpException) { return new Response( What you are looking for does not exist., 404); } $log->addError(json_encode(array( class => get_class($e), message => $e->getMessage(), code => $e->getCode(), trace => $e->getTrace(), ))); return new Response(Something very bad happened., 500); });Wednesday, March 2, 2011
  • 30. use MonologLogger; use MonologHandlerFileHandler; use SymfonyComponentHttpKernelExceptionNotFoundHttpException; $log = new Logger(myapp); $log->pushHandler(new FileHandler(__DIR__.log/error.log, Logger::ERROR)); $app->error(function(Exception $e) use ($log) { if ($e instanceof NotFoundHttpException) { return new Response( What you are looking for does not exist., 404); } $log->addError(json_encode(array( class => get_class($e), message => $e->getMessage(), code => $e->getCode(), trace => $e->getTrace(), ))); return new Response(Something very bad happened., 500); });Wednesday, March 2, 2011
  • 31. use MonologLogger; use MonologHandlerFileHandler; use SymfonyComponentHttpKernelExceptionNotFoundHttpException; $log = new Logger(myapp); $log->pushHandler(new FileHandler(__DIR__.log/error.log, Logger::ERROR)); $app->error(function(Exception $e) use ($log) { if ($e instanceof NotFoundHttpException) { return new Response( What you are looking for does not exist., 404); } $log->addError(json_encode(array( class => get_class($e), message => $e->getMessage(), code => $e->getCode(), trace => $e->getTrace(), ))); return new Response(Something very bad happened., 500); });Wednesday, March 2, 2011
  • 32. use MonologLogger; use MonologHandlerFileHandler; use SymfonyComponentHttpKernelExceptionNotFoundHttpException; $log = new Logger(myapp); $log->pushHandler(new FileHandler(__DIR__.log/error.log, Logger::ERROR)); $app->error(function(Exception $e) use ($log) { if ($e instanceof NotFoundHttpException) { return new Response( What you are looking for does not exist., 404); } $log->addError(json_encode(array( class => get_class($e), message => $e->getMessage(), code => $e->getCode(), trace => $e->getTrace(), ))); return new Response(Something very bad happened., 500); });Wednesday, March 2, 2011
  • 33. use MonologLogger; use MonologHandlerFileHandler; use SymfonyComponentHttpKernelExceptionNotFoundHttpException; $log = new Logger(myapp); $log->pushHandler(new FileHandler(__DIR__.log/error.log, Logger::ERROR)); $app->error(function(Exception $e) use ($log) { if ($e instanceof NotFoundHttpException) { return new Response( What you are looking for does not exist., 404); } $log->addError(json_encode(array( class => get_class($e), message => $e->getMessage(), code => $e->getCode(), trace => $e->getTrace(), ))); return new Response(Something very bad happened., 500); });Wednesday, March 2, 2011
  • 34. How? • Symfony2 • Extends HttpKernel • Event Dispatcher • Routing • Request & ResponseWednesday, March 2, 2011
  • 35. Experimental Fork • String return value • Syntactic sugar for routes • Before & after hooks • Error handling • Integration testsWednesday, March 2, 2011
  • 36. MicroKernelBundle • Bulat Shakirzyanow (avalanche123) • More routing flexibility • Uses instance of Kernel • Integrates with Symfony2Wednesday, March 2, 2011
  • 37. RecipesWednesday, March 2, 2011
  • 38. #1 File structure • app.php • bootstrap.php • index.phpWednesday, March 2, 2011
  • 39. app.php require __DIR__./bootstrap.php; use SilexFramework; $app = new Framework(); /* routes */ return $app;Wednesday, March 2, 2011
  • 40. bootstrap.php require_once __DIR__./silex.phar; // custom setup...Wednesday, March 2, 2011
  • 41. index.php $app = require __DIR__./app.php; $app->run();Wednesday, March 2, 2011
  • 42. #2 Service ContainerWednesday, March 2, 2011
  • 43. bootstrap.php use ...DependencyInjectionContainerBuilder; use ...DependencyInjectionLoaderYamlFileLoader; use ...ConfigFileLocator; $container = new ContainerBuilder(); $env = getenv(APP_ENV) ?: dev; $loader = new YamlFileLoader($container, new FileLocator(__DIR__)); $loader->load("config.$env.yml");Wednesday, March 2, 2011
  • 44. config.dev.yml services: silex: @silex.framework silex.framework: class: SilexFrameworkWednesday, March 2, 2011
  • 45. app.php $app = $container->get(silex);Wednesday, March 2, 2011
  • 46. config.dev.yml parameters: mongo.dbname: myapp services: mongo.users: class: MongoCollection arguments: [@mongo.db, user] mongo.db: class: MongoDB arguments: [@mongo, %mongo.dbname%] mongo: class: MongoWednesday, March 2, 2011
  • 47. app.php $app->get(/user/{id}, function($id) use ($container) { $users = $container->get(mongo.users); $user = $user->findOne(array("_id" => $id)); return new Response( json_encode($user), 200, array(Content-Type => application/json) ); });Wednesday, March 2, 2011
  • 48. #3 TwigWednesday, March 2, 2011
  • 49. bootstrap.php use SymfonyComponentClassLoaderUniversalClassLoader; $loader = new UniversalClassLoader(); $loader->registerPrefixes(array( Twig_ => __DIR__ . /vendor/Twig/lib, )); $loader->register(); $twigLoader = new Twig_Loader_Filesystem(/views); $twig = new Twig_Environment($twigLoader, array( cache => /cache/twig, debug => true, ));Wednesday, March 2, 2011
  • 50. app.php $app->get(/about, function() use ($twig) { $template = $twig->loadTemplate(about.html); return $template->render(array()); });Wednesday, March 2, 2011
  • 51. Wednesday, March 2, 2011
  • 52. #4 TestingWednesday, March 2, 2011
  • 53. use SilexWebTestCase; require_once __DIR__./silex.phar; class test extends WebTestCase { public function createApp() { return require __DIR__./app.php; } // tests... }Wednesday, March 2, 2011
  • 54. public function testAbout() { $client = $this->createClient(); $client->request(GET, /about); $response = $client->getResponse(); $this->assertTrue($response->isOk()); $this->assertContains(trashbin, $response->getContent()); $this->assertContains(github, $response->getContent()); $this->assertContains(igorw, $response->getContent()); }Wednesday, March 2, 2011
  • 55. $ phpunit test.phpWednesday, March 2, 2011
  • 56. fork me on github igorw/SilexWednesday, March 2, 2011
  • 57. ΩWednesday, March 2, 2011
  • 58. Questions?Wednesday, March 2, 2011

×