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

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 ManagementEric Hamilton
 
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 extensionsMark Baker
 
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)Amazon Web Services
 
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 BodarevElixir Club
 
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 ElixirYurii Bodarev
 
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
 
Micropage in microtime using microframework
Micropage in microtime using microframeworkMicropage in microtime using microframework
Micropage in microtime using microframeworkRadek Benkel
 
Interceptors: Into the Core of Pedestal
Interceptors: Into the Core of PedestalInterceptors: Into the Core of Pedestal
Interceptors: Into the Core of PedestalKent Ohashi
 
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 FrameworkDaniel Spector
 
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) Zend by Rogue Wave Software
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For BeginnersJonathan Wage
 
Silex: From nothing to an API
Silex: From nothing to an APISilex: From nothing to an API
Silex: From nothing to an APIchrisdkemper
 
Baruco 2014 - Rubymotion Workshop
Baruco 2014 - Rubymotion WorkshopBaruco 2014 - Rubymotion Workshop
Baruco 2014 - Rubymotion WorkshopBrian Sam-Bodden
 
PHP: The easiest language to learn.
PHP: The easiest language to learn.PHP: The easiest language to learn.
PHP: The easiest language to learn.Binny V A
 
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"Fwdays
 
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 triangleAkihito Koriyama
 

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

Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 

Recently uploaded (20)

Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 

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