SlideShare una empresa de Scribd logo
1 de 41
© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
Development area
                                                                 meeting #4/2011




© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
SILEX
                                                                 PHP Micro-framework




© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
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 GroupM Company
SILEX
                                                                     http://silex-project.org/
                                                                 https://github.com/fabpot/Silex




© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
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
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
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
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
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
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
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
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
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 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
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 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
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
                                          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
Routing


© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
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
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
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
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
Default Values

                   $app->get('/{pageName}', function ($pageName) {
                       ...
                   })
                   ->value('pageName', 'index');




© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
Applicazioni Riusabili


© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
// 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
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
 •       TranslationExtension
 •       UrlGeneratorExtension
 •       ValidatorExtension


                                                                 Altre implementabili attraverso API
© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
Testing

© H-art 2011 | All Rights Reserved | H-art is a GroupM Company
... 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
... 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
... 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
... 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
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://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

Más contenido relacionado

Similar a Introduzione a Silex

Introducing Applitude: Simple Module Management
Introducing Applitude: Simple Module ManagementIntroducing Applitude: Simple Module Management
Introducing Applitude: Simple Module Management
Eric Hamilton
 
The symfony platform: Create your very own framework (PHP Quebec 2008)
The symfony platform: Create your very own framework (PHP Quebec 2008)The symfony platform: Create your very own framework (PHP Quebec 2008)
The symfony platform: Create your very own framework (PHP Quebec 2008)
Fabien Potencier
 
Silex: From nothing to an API
Silex: From nothing to an APISilex: From nothing to an API
Silex: From nothing to an API
chrisdkemper
 
Baruco 2014 - Rubymotion Workshop
Baruco 2014 - Rubymotion WorkshopBaruco 2014 - Rubymotion Workshop
Baruco 2014 - Rubymotion Workshop
Brian Sam-Bodden
 

Similar a Introduzione a Silex (20)

Building web APIs in PHP with Zend Expressive
Building web APIs in PHP with Zend ExpressiveBuilding web APIs in PHP with Zend Expressive
Building web APIs in PHP with Zend Expressive
 
Introducing Applitude: Simple Module Management
Introducing Applitude: Simple Module ManagementIntroducing Applitude: Simple Module Management
Introducing Applitude: Simple Module Management
 
Silex Cheat Sheet
Silex Cheat SheetSilex Cheat Sheet
Silex Cheat Sheet
 
Silex Cheat Sheet
Silex Cheat SheetSilex Cheat Sheet
Silex Cheat Sheet
 
Develop microservices in php
Develop microservices in phpDevelop microservices in php
Develop microservices in php
 
Zephir - A Wind of Change for writing PHP extensions
Zephir - A Wind of Change for writing PHP extensionsZephir - A Wind of Change for writing PHP extensions
Zephir - A Wind of Change for writing PHP extensions
 
AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)
AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)
AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)
 
Ecto and Phoenix: Doing web with Elixir - Yurii Bodarev
Ecto and Phoenix: Doing web with Elixir - Yurii BodarevEcto and Phoenix: Doing web with Elixir - Yurii Bodarev
Ecto and Phoenix: Doing web with Elixir - Yurii Bodarev
 
Ecto and Phoenix: Doing Web With Elixir
Ecto and Phoenix: Doing Web With ElixirEcto and Phoenix: Doing Web With Elixir
Ecto and Phoenix: Doing Web With Elixir
 
The symfony platform: Create your very own framework (PHP Quebec 2008)
The symfony platform: Create your very own framework (PHP Quebec 2008)The symfony platform: Create your very own framework (PHP Quebec 2008)
The symfony platform: Create your very own framework (PHP Quebec 2008)
 
Micropage in microtime using microframework
Micropage in microtime using microframeworkMicropage in microtime using microframework
Micropage in microtime using microframework
 
Interceptors: Into the Core of Pedestal
Interceptors: Into the Core of PedestalInterceptors: Into the Core of Pedestal
Interceptors: Into the Core of Pedestal
 
Crossing the Bridge: Connecting Rails and your Front-end Framework
Crossing the Bridge: Connecting Rails and your Front-end FrameworkCrossing the Bridge: Connecting Rails and your Front-end Framework
Crossing the Bridge: Connecting Rails and your Front-end Framework
 
Speed up web APIs with Expressive and Swoole (PHP Day 2018)
Speed up web APIs with Expressive and Swoole (PHP Day 2018) Speed up web APIs with Expressive and Swoole (PHP Day 2018)
Speed up web APIs with Expressive and Swoole (PHP Day 2018)
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For Beginners
 
Silex: From nothing to an API
Silex: From nothing to an APISilex: From nothing to an API
Silex: From nothing to an API
 
Baruco 2014 - Rubymotion Workshop
Baruco 2014 - Rubymotion WorkshopBaruco 2014 - Rubymotion Workshop
Baruco 2014 - Rubymotion Workshop
 
PHP: The easiest language to learn.
PHP: The easiest language to learn.PHP: The easiest language to learn.
PHP: The easiest language to learn.
 
Tobias Nyholm "Deep dive into Symfony 4 internals"
Tobias Nyholm "Deep dive into Symfony 4 internals"Tobias Nyholm "Deep dive into Symfony 4 internals"
Tobias Nyholm "Deep dive into Symfony 4 internals"
 
A resource oriented framework using the DI/AOP/REST triangle
A resource oriented framework using the DI/AOP/REST triangleA resource oriented framework using the DI/AOP/REST triangle
A resource oriented framework using the DI/AOP/REST triangle
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Último (20)

Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 

Introduzione a Silex

  • 1. © H-art 2011 | All Rights Reserved | H-art is a GroupM Company
  • 2. Development area meeting #4/2011 © H-art 2011 | All Rights Reserved | H-art is a GroupM Company
  • 3. SILEX PHP Micro-framework © H-art 2011 | All Rights Reserved | H-art is a GroupM Company
  • 4. Micro-che ? © H-art 2011 | All Rights Reserved | H-art is a GroupM Company
  • 5. Micro-framework • Funzionalità minime • Leggeri • Semplici © H-art 2011 | All Rights Reserved | H-art is a GroupM Company
  • 6. Perfetti quando un framework è “troppa roba” © H-art 2011 | All Rights Reserved | H-art is a GroupM Company
  • 7. SILEX http://silex-project.org/ https://github.com/fabpot/Silex © H-art 2011 | All Rights Reserved | H-art is a GroupM Company
  • 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. 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. 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. 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. 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. 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. 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. 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. Un pò di più ? © H-art 2011 | All Rights Reserved | H-art is a GroupM Company
  • 17. Before() e after() © H-art 2011 | All Rights Reserved | H-art is a GroupM Company
  • 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. Gestione degli errori © H-art 2011 | All Rights Reserved | H-art is a GroupM Company
  • 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. Escaping © H-art 2011 | All Rights Reserved | H-art is a GroupM Company
  • 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. Routing © H-art 2011 | All Rights Reserved | H-art is a GroupM Company
  • 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. 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. 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. 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. Default Values $app->get('/{pageName}', function ($pageName) { ... }) ->value('pageName', 'index'); © H-art 2011 | All Rights Reserved | H-art is a GroupM Company
  • 29. Applicazioni Riusabili © H-art 2011 | All Rights Reserved | H-art is a GroupM Company
  • 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. Ancora non basta ? © H-art 2011 | All Rights Reserved | H-art is a GroupM Company
  • 32. Extensions © H-art 2011 | All Rights Reserved | H-art is a GroupM Company
  • 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. Testing © H-art 2011 | All Rights Reserved | H-art is a GroupM Company
  • 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. ... 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. ... 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. ... 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. Q&A © H-art 2011 | All Rights Reserved | H-art is a GroupM Company
  • 40. Risorse © H-art 2011 | All Rights Reserved | H-art is a GroupM Company
  • 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

Notas del editor

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n