Introduzione a Silex

3,957 views
3,859 views

Published on

Presentazione introduttiva a Silex tenuta in occasione del 4 meeting mensile area Dev H-art.

Published in: Technology, Business
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,957
On SlideShare
0
From Embeds
0
Number of Embeds
2,273
Actions
Shares
0
Downloads
0
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Introduzione a Silex

    1. 1. © H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    2. 2. Development area meeting #4/2011© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    3. 3. SILEX PHP Micro-framework© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    4. 4. Micro-che ?© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    5. 5. Micro-framework • Funzionalità minime • Leggeri • Semplici© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    6. 6. Perfetti quando un framework è “troppa roba”© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    7. 7. SILEX http://silex-project.org/ https://github.com/fabpot/Silex© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    8. 8. Perchè silex ? • 400 KB • Ottima implementazione • Basato su symfony 2 (riuso di componenti e knowledge) • Customizzabile con estensioni© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    9. 9. SILEX require_once __DIR__./silex.phar; $app = new SilexApplication(); $app->get(/hello/{name}, function($name) { return "Hello $name"; }); $app->run();© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    10. 10. SILEX require_once __DIR__./silex.phar; $app = new SilexApplication(); Il Framework $app->get(/hello/{name}, function($name) { return "Hello $name"; }); $app->run();© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    11. 11. SILEX require_once __DIR__./silex.phar; $app = new SilexApplication(); L’ applicazione $app->get(/hello/{name}, function($name) { return "Hello $name"; }); $app->run();© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    12. 12. SILEX require_once __DIR__./silex.phar; $app = new SilexApplication(); $app->get(/hello/{name}, function($name) { return "Hello $name"; }); $app->run(); La action© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    13. 13. Una route SILEX require_once __DIR__./silex.phar; $app = new SilexApplication(); $app->get(/hello/{name}, function($name) { return "Hello $name"; }); $app->run(); La action© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    14. 14. Route SILEX Controller require_once __DIR__./silex.phar; $app = new SilexApplication(); $app->get(/hello/{name}, function($name) { return "Hello $name"; }); $app->run(); La action© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    15. 15. SILEX require_once __DIR__./silex.phar; $app = new SilexApplication(); $app->get(/hello/{name}, function($name) { return "Hello $name"; }); E si balla!! $app->run();© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    16. 16. Un pò di più ?© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    17. 17. Before() e after()© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    18. 18. Posso definire dei comportamenti o delle operazioni da eseguire prima e dopo ogni action passando delle closure ai filtri before e after $app->before(function () { // attivo una connesione a DB ? // carico qualche layout generico ? }); $app->after(function () { // chiudo connessione a DB ? // });© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    19. 19. Gestione degli errori© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    20. 20. Posso definire dei comportamenti in caso di errore per fare in modo che l’applicazione li notifichi in maniera “decente” use SymfonyComponentHttpFoundationResponse; use SymfonyComponentHttpKernelExceptionHttpException; use SymfonyComponentHttpKernelExceptionNotFoundHttpException; $app->error(function (Exception $e) { if ($e instanceof NotFoundHttpException) { return new Response(The requested page could not be found., 404); } $code = ($e instanceof HttpException) ? $e->getStatusCode() : 500; return new Response(We are sorry, but something went terribly wrong., $code); });© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    21. 21. Escaping© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    22. 22. Silex mette a disposizione il metodo escape() per ottenere l’escaping delle variabili $app->get(/name, function () use ($app) { $name = $app[request]->get(name); return "You provided the name {$app- >escape($name)}."; });© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    23. 23. Routing© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    24. 24. Variabili $app->get(/blog/show/{id}, function ($id) { ... }); $app->get(/blog/show/{postId}/{commentId}, function ($postId, $commentId) { ... }); $app->get(/blog/show/{postId}/{commentId}, function ($commentId, $postId) { ... });© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    25. 25. Converter $app->get(/user/{id}, function ($id) { // ... })->convert(id, function ($id) { return (int) $id; }); Il parametro $id viene passato alla closure e non alla action che riceve invece il valore restituito dalla closure© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    26. 26. Converter $userProvider = function ($id) { return new User($id); }; $app->get(/user/{user}, function (User $user) { // ... })->convert(user, $userProvider); $app->get(/user/{user}/edit, function (User $user) { // ... })->convert(user, $userProvider);© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    27. 27. Requirements $app->get(/blog/show/{postId}/{commentId}, function ($postId, $commentId) { ... }) ->assert(postId, d+) ->assert(commentId, d+);© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    28. 28. Default Values $app->get(/{pageName}, function ($pageName) { ... }) ->value(pageName, index);© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    29. 29. Applicazioni Riusabili© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    30. 30. // blog.php require_once __DIR__./silex.phar; $app = new SilexApplication(); // define your blog app $app->get(/post/{id}, function ($id) { ... }); // return the app instance return $app; $blog = require __DIR__./blog.php; $app = new SilexApplication(); $app->mount(/blog, $blog); $app->run();© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    31. 31. Ancora non basta ?© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    32. 32. Extensions© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    33. 33. Extensions incluse • DoctrineExtension • MonologExtension • SessionExtension • TwigExtension • TranslationExtension • UrlGeneratorExtension • ValidatorExtension Altre implementabili attraverso API© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    34. 34. Testing© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    35. 35. ... class FooAppTest extends WebTestCase public function testInitialPage() { $client = $this->createClient(); $crawler = $client->request(GET, /); $this->assertTrue($client->getResponse()->isOk()); $this->assertEquals(1, count($crawler- >filter(h1:contains("Contact us")))); $this->assertEquals(1, count($crawler- >filter(form))); ... }© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    36. 36. ... class FooAppTest extends WebTestCase Il browser public function testInitialPage() { $client = $this->createClient(); $crawler = $client->request(GET, /); $this->assertTrue($client->getResponse()->isOk()); $this->assertEquals(1, count($crawler- >filter(h1:contains("Contact us")))); $this->assertEquals(1, count($crawler- >filter(form))); ... }© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    37. 37. ... class FooAppTest extends WebTestCase Il parser della pagina public function testInitialPage() { $client = $this->createClient(); $crawler = $client->request(GET, /); $this->assertTrue($client->getResponse()->isOk()); $this->assertEquals(1, count($crawler- >filter(h1:contains("Contact us")))); $this->assertEquals(1, count($crawler- >filter(form))); ... }© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    38. 38. ... class FooAppTest extends WebTestCase Verifiche su contenuto e public function testInitialPage() Response { $client = $this->createClient(); $crawler = $client->request(GET, /); $this->assertTrue($client->getResponse()->isOk()); $this->assertEquals(1, count($crawler- >filter(h1:contains("Contact us")))); $this->assertEquals(1, count($crawler- >filter(form))); ... }© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    39. 39. Q&A© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    40. 40. Risorse© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
    41. 41. • http://silex-project.org/documentation• http://www.slideshare.net/IgorWiedler/silex-the-symfony2-microframework• http://codenugget.org/5-reasons-why-silex-is-king-of-all-php-micro• https://github.com/igorw/silex-examples• https://github.com/helios-ag/Silex-Upload-File-Example© H-art 2011 | All Rights Reserved | H-art is a GroupM Company

    ×