SlideShare a Scribd company logo
© 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

More Related Content

Similar to 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 to 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
 

Recently uploaded

Recently uploaded (20)

WSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptxWSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptx
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
Agentic RAG What it is its types applications and implementation.pdf
Agentic RAG What it is its types applications and implementation.pdfAgentic RAG What it is its types applications and implementation.pdf
Agentic RAG What it is its types applications and implementation.pdf
 
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
 
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
 
What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024
 
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCustom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří Karpíšek
 
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
In-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsIn-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT Professionals
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara Laskowska
 

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

Editor's Notes

  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