Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
Development area                                                                 meeting #4/2011© H-art 2011 | All Rights ...
SILEX                                                                 PHP Micro-framework© H-art 2011 | All Rights Reserve...
Micro-che ?© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
Micro-framework • Funzionalità minime • Leggeri • Semplici© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
Perfetti quando un framework è                          “troppa roba”© H-art 2011 | All Rights Reserved | H-art is a Group...
SILEX                                                                     http://silex-project.org/                       ...
Perchè silex ? • 400 KB • Ottima implementazione • Basato su symfony 2 (riuso di componenti e knowledge) • Customizzabile ...
SILEX    require_once __DIR__./silex.phar;    $app = new SilexApplication();    $app->get(/hello/{name}, function($name) {...
SILEX    require_once __DIR__./silex.phar;    $app = new SilexApplication();                                      Il Frame...
SILEX    require_once __DIR__./silex.phar;    $app = new SilexApplication();                                      L’ appli...
SILEX    require_once __DIR__./silex.phar;    $app = new SilexApplication();    $app->get(/hello/{name}, function($name) {...
Una route                                                                 SILEX    require_once __DIR__./silex.phar;    $a...
Route                                                                 SILEX                                               ...
SILEX    require_once __DIR__./silex.phar;    $app = new SilexApplication();    $app->get(/hello/{name}, function($name) {...
Un pò di più ?© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
Before() e after()© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
Posso definire dei comportamenti o delle operazioni da eseguire prima e dopo            ogni action passando delle closure...
Gestione degli errori© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
Posso definire dei comportamenti in caso di errore per fare in modo che                         l’applicazione li notifich...
Escaping© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
Silex mette a disposizione il metodo escape() per ottenere l’escaping delle                                          varia...
Routing© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
Variabili                   $app->get(/blog/show/{id}, function ($id) {                       ...                   });   ...
Converter                   $app->get(/user/{id}, function ($id) {                       // ...                   })->conv...
Converter                   $userProvider = function ($id) {                       return new User($id);                  ...
Requirements                   $app->get(/blog/show/{postId}/{commentId},                   function ($postId, $commentId)...
Default Values                   $app->get(/{pageName}, function ($pageName) {                       ...                  ...
Applicazioni Riusabili© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
// blog.php                   require_once __DIR__./silex.phar;                   $app = new SilexApplication();          ...
Ancora non basta ?© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
Extensions© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
Extensions incluse •       DoctrineExtension •       MonologExtension •       SessionExtension •       TwigExtension •    ...
Testing© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
... class FooAppTest extends WebTestCase      public function testInitialPage()      {          $client = $this->createCli...
... class FooAppTest extends WebTestCase                                                                     Il browser   ...
... class FooAppTest extends WebTestCase                                                                 Il parser della p...
... class FooAppTest extends WebTestCase                                        Verifiche su contenuto e      public functi...
Q&A© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
Risorse© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
• http://silex-project.org/documentation• http://www.slideshare.net/IgorWiedler/silex-the-symfony2-microframework• http://...
Upcoming SlideShare
Loading in …5
×

Introduzione a Silex

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

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all
  • Login to see the comments

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

×