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

Último (20)

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
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
 
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
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
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
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
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...
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 

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