SlideShare une entreprise Scribd logo
1  sur  53
Télécharger pour lire hors ligne
1
2
PAGE




 3
PAGE
TIME



 4
PAGE
TIME
FRAMEWORK


5
name: Radosław Benkel
                          nick: singles
                          www: http://www.rbenkel.me
                          twitter: @singlespl *




* and I have nothing in common with http://www.singles.pl ;]

                                   6
SOMETIMES, FULL STACK
  FRAMEWORK IS AN
     OVERHEAD



         7
THIS IS WHY WE HAVE
MICROFRAMEWORKS



        8
9
➜


10
USUALLY, DOES SMALL
 AMOUT OF THINGS.



        11
USUALLY, DOES SMALL
  AMOUT OF THINGS.

ROUTING




          12
USUALLY, DOES SMALL
  AMOUT OF THINGS.

ROUTING
          HTTP CACHING



           13
USUALLY, DOES SMALL
  AMOUT OF THINGS.

ROUTING
          HTTP CACHING

TEMPLATES

            14
15
Ihope,because...


              16
640Koughttobe
enoughforanyone.


                         17
640Koughttobe
             enoughforanyone.
      BTW.Probablyhedidn'tsaythat:
HTTP://QUOTEINVESTIGATOR.COM/2011/09/08/640K-ENOUGH/



                                      18
OK,OK,Iwantmeat!
          readas:Showmesomecodeplease




                                              19
Littleframework
                   =
littleamountofmeat

                     20
I'LL USE




http://www.slimframework.com/


             21
BUT THERE ARE OTHERS:
          http://flightphp.com/




          http://silex.sensiolabs.org/




              http://www.limonade-php.net




         22
SLIM EXAMPLES




      23
BASE ROUTING


?php

require 'Slim/Slim.php';

$app = new Slim();

$app-get('/', function() {
    echo 'Hello World from base route.';
});

$app-run();




                                    24
REQUIRED PARAM

?php

require 'Slim/Slim.php';

$app = new Slim();

$app-get('/', function() {
    echo 'Hello World from base route.';
});

//param name is required
$app-get('/hello_to/:name', function($name) {
    echo 'Hello World to ' . $name;
});

$app-run();




                                    25
OPTIONAL PARAM
?php

require 'Slim/Slim.php';

$app = new Slim();

$app-get('/', function() {
    echo 'Hello World from base route.';
});

//when using optional params, you have to define default value for function
param
$app-get('/hello_to(/:name)', function($name = null) {
    if ($name === null) {
        $name = 'John Doe';
    }
    echo 'Hello World to ' . $name;
});

$app-run();


                                    26
NAMED ROUTES
?php

require 'Slim/Slim.php';

$app = new Slim();

$app-get('/', function() use ($app) {
    echo 'Hello World from base route.br';
    $url = $app-urlFor('hello', array('name' = 'Jimmy')); //create link for route
    $link = sprintf('a href=%s%s/a', $url, $url);
    echo 'Oh, link to hello page for Jimmy is ' . $link;
});

$app-get('/hello_to(/:name)', function($name = null) {
    if ($name === null) {
        $name = 'John Doe';
    }
    echo 'Hello World to ' . $name;
})-name('hello'); //using name for route

$app-run();




                                         27
ROUTE CONDITIONS
?php

require 'Slim/Slim.php';

$app = new Slim();

$app-get('/', function() use ($app) {
    echo 'Hello World from base route.br';
    $url = $app-urlFor('hello', array('name' = 'Jimmy'));
    $link = sprintf('a href=%s%s/a', $url, $url);
    echo 'Oh, link to hello page for Jimmy is ' . $link;
});

$app-get('/hello_to(/:name)', function($name = null) {
    if ($name === null) {
        $name = 'John Doe';
    }
    echo 'Hello World to ' . $name;
})-name('hello')
  -conditions(array('name' = '[A-Za-z]+')); //use only letters as param 'name'

$app-run();




                                         28
REDIRECT
?php

require 'Slim/Slim.php';

$app = new Slim();

/* ... */

$app-get('/hello_to(/:name)', function($name = null) {
    if ($name === null) {
        $name = 'John Doe';
    }
    echo 'Hello World to ' . $name;
})-name('hello')
  -conditions(array('name' = '[A-Za-z]+'));

//redirect to default hello page
$app-get('/redirect', function() use ($app) {
    $app-redirect($app-urlFor('hello'));
});

$app-run();




                                         29
REDIRECT WITH STATUS
?php

require 'Slim/Slim.php';

$app = new Slim();

/* ... */

$app-get('/hello_to(/:name)', function($name = null) {
    if ($name === null) {
        $name = 'John Doe';
    }
    echo 'Hello World to ' . $name;
})-name('hello')
  -conditions(array('name' = '[A-Za-z]+'));

//redirect to default hello page as 301, not 302 which is default
$app-get('/redirect', function() use ($app) {
    $app-redirect($app-urlFor('hello'), 301);
});

$app-run();




                                         30
MIDDLEWARE

?php

require 'Slim/Slim.php';

$app = new Slim();

$app-get('/',
    function() {
        //this will be executed before main callable
        echo Hello, I'm middleware br;
    },
    function() use ($app) {
        echo 'Hello World from base route.br';
        $url = $app-urlFor('hello', array('name' = 'Jimmy'));
        $link = sprintf('a href=%s%s/a', $url, $url);
        echo 'Oh, link to hello page for Jimmy is ' . $link;
});
/* ... */
$app-run();




                                         31
MIDDLEWARE
?php

require 'Slim/Slim.php';

$app = new Slim();

$app-get('/',
    function() {
        //this will be executed before main callable
        echo Hello, I'm middleware br;
    },
    function() {
        //this will be executed before main callable
        echo And I'm second middleware br;
    },
    function() use ($app) {
        echo 'Hello World from base route.br';
        $url = $app-urlFor('hello', array('name' = 'Jimmy'));
        $link = sprintf('a href=%s%s/a', $url, $url);
        echo 'Oh, link to hello page for Jimmy is ' . $link;
});
/* ... */
$app-run();



                                         32
MIDDLEWARE
?php

require 'Slim/Slim.php';

$app = new Slim();

$app-get('/',
    function() {
        //this will be executed before main callable
        echo Hello, I'm middleware br;
    },
    function() {
        //this will be executed before main callable
        echo And I'm second middleware br;
    },
    function() use ($app) {
        echo 'Hello World from base route.br';

   Andsoon-everythingbeforelastcallableis
        $url = $app-urlFor('hello', array('name' = 'Jimmy'));
        $link = sprintf('a href=%s%s/a', $url, $url);

                                               middleware
        echo 'Oh, link to hello page for Jimmy is ' . $link;
});
/* ... */
$app-run();



                                                      33
VIEW
?php
//file index.php

require 'Slim/Slim.php';

$app = new Slim();

$app-get('/', function() use ($app) {
        $url = $app-urlFor('hello', array('name' = 'Jimmy'));
        //default path is __DIR__ . /templates
        return $app-render('view.php', compact('url'));
});
/* ... */
$app-run();




Hello World from base route. br
Oh, link to hello page for Jimmy is a href=?php echo $url??php echo
$url?/a




                                         34
HTTP CACHE - ETAG
?php

require 'Slim/Slim.php';

$app = new Slim();

/* ... */

$app-get('/hello_to(/:name)', function($name = null) use ($app) {
    if ($name === null) {
        $name = 'John Doe';
    }
    //auto ETag based on some id - next request with the same name will return 304
Not Modified
    $app-etag($name);
    echo 'Hello World to ' . $name;
})-name('hello')
  -conditions(array('name' = '[A-Za-z]+'));

/* ... */

$app-run();



                                         35
HTTP CACHE - TIME BASED

?php

require 'Slim/Slim.php';

$app = new Slim();

/* ... */

$app-get('/hello_to(/:name)', function($name = null) use ($app) {
    if ($name === null) {
        $name = 'John Doe';
    }
    $app-lastModified(1327305485); //cache based on time
    echo 'Hello World to ' . $name;
})-name('hello')
  -conditions(array('name' = '[A-Za-z]+'));

/* ... */

$app-run();




                                         36
FLASH MESSAGE
?php

require 'Slim/Slim.php';

$app = new Slim();

$app-get('/', function() use ($app) {
        $url = $app-urlFor('hello', array('name' = 'Jimmy'));
        return $app-render('view.php', compact('url'));
});

//redirect to default page with flash message which will be displayed once
$app-get('/redirect', function() use ($app) {
    $app-flash('info', You were redirected);
    $app-redirect($app-request()-getRootUri());
});

$app-run();




?php echo $flash['info'] ?
Hello World from base route. br
Oh, link to hello page for Jimmy is a href=?php echo $url??php echo $url?/a



                                               37
CUSTOM 404
?php

require 'Slim/Slim.php';

$app = new Slim();

//define custom 404 page
$app-notFound(function() {
    echo I'm custom 404;
});

$app-get('/hello_to(/:name)', function($name = null) use ($app) {
    if ($name === null) {
        $name = 'John Doe';
    }
    $possibleNames = array('Leonard', 'Sheldon', 'John Doe');
    //when name not found, force 404 page
    if (array_search($name, $possibleNames) === false) {
        $app-notFound();
    }
    echo 'Hello World to ' . $name;
})-name('hello')
  -conditions(array('name' = '[A-Za-z]+'));

$app-run();



                                            38
CUSTOM 404
?php

require 'Slim/Slim.php';

$app = new Slim();

//define custom 404 page
$app-notFound(function() {
    echo I'm custom 404;
});

$app-get('/hello_to(/:name)', function($name = null) use ($app) {
    if ($name === null) {

    }      Customerrorpage(500)alsopossible
        $name = 'John Doe';

    $possibleNames = array('Leonard', 'Sheldon', 'John Doe');
    //when name not found, force 404 page
    if (array_search($name, $possibleNames) === false) {
        $app-notFound();
    }
    echo 'Hello World to ' . $name;
})-name('hello')
  -conditions(array('name' = '[A-Za-z]+'));

$app-run();



                                             39
REST PATHS #1


?php

require 'Slim/Slim.php';

$app = new Slim();
//method name maps to HTTP method
$app-get('/article'), function(/* ... */) {});
$app-post('/article'), function(/* ... */) {});
$app-get('/article/:id/'), function(/* ... */) {});
$app-put('/article/:id/'), function(/* ... */) {});
$app-delete('/article/:id/'), function(/* ... */) {});




                                    40
REST PATHS #2
?php

require 'Slim/Slim.php';

$app = new Slim();

//same as previous one
$app-map('/article'), function() use ($app) {
    if ($app-request()-isGet()) {
        /* ... */
    } else if ($app-request()-isPost() {
        /* ... */
    }) else {
        /* ... */
    }
})-via('GET', 'POST');
$app-map('/article/:id/'), function($id) use ($app) {
    //same as above
})-via('GET', 'PUT', 'DELETE');



                                    41
ALSO:
ENCRYPTED SESSIONS AND
        COOKIES,
  APPLICATION MODES,
   CUSTOM TEMPLATES
      AND MORE...

          42
http://www.slimframework.com/
    documentation/stable

             43
ButIcan'tusePHP5.3.
              Whatthen?

                               44
PHP 5.2
?php

require 'Slim/Slim.php';
$app = new Slim();

function index() {
    global $app;
    echo 'Hello World from base route.br';
    $url = $app-urlFor('hello', array('Jimmy'));
    $link = sprintf('a href=%s%s/a', $url, $url);
    echo 'Oh, link to hello page for Jimmy is ' . $link;
}

//last param must return true for is_callable call, so that it's valid
$app-get('/', 'index');

/* ... */

$app-run();



                                    45
PHP 5.2
?php

require 'Slim/Slim.php';
$app = new Slim();

function index() {
    global $app;
    echo 'Hello World from base route.br';
    $url = $app-urlFor('hello', array('Jimmy'));
    $link = sprintf('a href=%s%s/a', $url, $url);
    echo 'Oh, link to hello page for Jimmy is ' . $link;
}

//last param must return true for is_callable call, so that it's valid

 Somebodysaid,that:everytime,whenyouuse
$app-get('/', 'index');

/* ... */
                    global,unicorndies;)
$app-run();



                                             46
Source: http://tvtropes.org/pmwiki/pmwiki.php/Main/DeadUnicornTrope



                              47
Sook,secondapproach:


                     48
PHP 5.2
?php

class Controller {
    public static $app;
    public static function index() {
        echo 'Hello World from base route.br';
        $url = self::$app-urlFor('hello', array('Jimmy'));
        $link = sprintf('a href=%s%s/a', $url, $url);
        echo 'Oh, link to hello page for Jimmy is ' . $link;
    }
}

require 'Slim/Slim.php';

$app = new Slim();
Controller::$app = $app;

//last param must return true for is_callable call, so that it's also valid
$app-get('/', array('Controller', 'index'));
/* ... */
$app-run();




                                         49
ButIMHOthisoneisthe
            bestsolution:

                               50
PHP 5.2
?php

class Controller {
    protected $_app;
    public function __construct(Slim $app) {
        $this-_app = $app;
    }
    public function index() {
        echo 'Hello World from base route.br';
        $url = $this-_app-urlFor('hello', array('Jimmy'));
        $link = sprintf('a href=%s%s/a', $url, $url);
        echo 'Oh, link to hello page for Jimmy is ' . $link;
    }
}

require 'Slim/Slim.php';

$app = new Slim();
$controller = new Controller($app);

//last param must return true for is_callable call, so that it's also valid
$app-get('/', array($controller, 'index'));
/* ... */
$app-run();


                                         51
SO, DO YOU REALLY NEED
         THAT ?




  Source: http://www.rungmasti.com/2011/05/swiss-army-knife/

                            52
53

Contenu connexe

Tendances

Unit and Functional Testing with Symfony2
Unit and Functional Testing with Symfony2Unit and Functional Testing with Symfony2
Unit and Functional Testing with Symfony2
Fabien Potencier
 
symfony on action - WebTech 207
symfony on action - WebTech 207symfony on action - WebTech 207
symfony on action - WebTech 207
patter
 
jQuery Plugin Creation
jQuery Plugin CreationjQuery Plugin Creation
jQuery Plugin Creation
benalman
 
Interface de Voz con Rails
Interface de Voz con RailsInterface de Voz con Rails
Interface de Voz con Rails
Svet Ivantchev
 
Silex: From nothing to an API
Silex: From nothing to an APISilex: From nothing to an API
Silex: From nothing to an API
chrisdkemper
 
PHP an intro -1
PHP an intro -1PHP an intro -1
PHP an intro -1
Kanchilug
 
Desymfony 2011 - Habemus Bundles
Desymfony 2011 - Habemus BundlesDesymfony 2011 - Habemus Bundles
Desymfony 2011 - Habemus Bundles
Albert Jessurum
 

Tendances (20)

Report: Avalanche 'very likely' to host outdoor game at Coors Field
Report: Avalanche 'very likely' to host outdoor game at Coors FieldReport: Avalanche 'very likely' to host outdoor game at Coors Field
Report: Avalanche 'very likely' to host outdoor game at Coors Field
 
Mojolicious. Веб в коробке!
Mojolicious. Веб в коробке!Mojolicious. Веб в коробке!
Mojolicious. Веб в коробке!
 
Mojolicious: what works and what doesn't
Mojolicious: what works and what doesn'tMojolicious: what works and what doesn't
Mojolicious: what works and what doesn't
 
PHP Tips & Tricks
PHP Tips & TricksPHP Tips & Tricks
PHP Tips & Tricks
 
Unit and Functional Testing with Symfony2
Unit and Functional Testing with Symfony2Unit and Functional Testing with Symfony2
Unit and Functional Testing with Symfony2
 
Current state-of-php
Current state-of-phpCurrent state-of-php
Current state-of-php
 
RESTful web services
RESTful web servicesRESTful web services
RESTful web services
 
Mojolicious - A new hope
Mojolicious - A new hopeMojolicious - A new hope
Mojolicious - A new hope
 
Plugin jQuery, Design Patterns
Plugin jQuery, Design PatternsPlugin jQuery, Design Patterns
Plugin jQuery, Design Patterns
 
symfony on action - WebTech 207
symfony on action - WebTech 207symfony on action - WebTech 207
symfony on action - WebTech 207
 
jQuery Plugin Creation
jQuery Plugin CreationjQuery Plugin Creation
jQuery Plugin Creation
 
Interface de Voz con Rails
Interface de Voz con RailsInterface de Voz con Rails
Interface de Voz con Rails
 
Silex: From nothing to an API
Silex: From nothing to an APISilex: From nothing to an API
Silex: From nothing to an API
 
The promise of asynchronous php
The promise of asynchronous phpThe promise of asynchronous php
The promise of asynchronous php
 
PHP an intro -1
PHP an intro -1PHP an intro -1
PHP an intro -1
 
Building Your First Widget
Building Your First WidgetBuilding Your First Widget
Building Your First Widget
 
London XQuery Meetup: Querying the World (Web Scraping)
London XQuery Meetup: Querying the World (Web Scraping)London XQuery Meetup: Querying the World (Web Scraping)
London XQuery Meetup: Querying the World (Web Scraping)
 
Desymfony 2011 - Habemus Bundles
Desymfony 2011 - Habemus BundlesDesymfony 2011 - Habemus Bundles
Desymfony 2011 - Habemus Bundles
 
My app is secure... I think
My app is secure... I thinkMy app is secure... I think
My app is secure... I think
 
How to actually use promises - Jakob Mattsson, FishBrain
How to actually use promises - Jakob Mattsson, FishBrainHow to actually use promises - Jakob Mattsson, FishBrain
How to actually use promises - Jakob Mattsson, FishBrain
 

Similaire à Micropage in microtime using microframework

Aura Project for PHP
Aura Project for PHPAura Project for PHP
Aura Project for PHP
Hari K T
 
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applications
elliando dias
 
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applications
elliando dias
 

Similaire à Micropage in microtime using microframework (20)

Silex Cheat Sheet
Silex Cheat SheetSilex Cheat Sheet
Silex Cheat Sheet
 
Silex Cheat Sheet
Silex Cheat SheetSilex Cheat Sheet
Silex Cheat Sheet
 
Forget about index.php and build you applications around HTTP!
Forget about index.php and build you applications around HTTP!Forget about index.php and build you applications around HTTP!
Forget about index.php and build you applications around HTTP!
 
Perl Web Client
Perl Web ClientPerl Web Client
Perl Web Client
 
Forget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers CracowForget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers Cracow
 
Modern Web Development with Perl
Modern Web Development with PerlModern Web Development with Perl
Modern Web Development with Perl
 
Durian: a PHP 5.5 microframework with generator-style middleware
Durian: a PHP 5.5 microframework with generator-style middlewareDurian: a PHP 5.5 microframework with generator-style middleware
Durian: a PHP 5.5 microframework with generator-style middleware
 
Aura Project for PHP
Aura Project for PHPAura Project for PHP
Aura Project for PHP
 
Nashvile Symfony Routes Presentation
Nashvile Symfony Routes PresentationNashvile Symfony Routes Presentation
Nashvile Symfony Routes Presentation
 
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applications
 
PSR-7, middlewares e o futuro dos frameworks
PSR-7, middlewares e o futuro dos frameworksPSR-7, middlewares e o futuro dos frameworks
PSR-7, middlewares e o futuro dos frameworks
 
Symfony components in the wild, PHPNW12
Symfony components in the wild, PHPNW12Symfony components in the wild, PHPNW12
Symfony components in the wild, PHPNW12
 
The promise of asynchronous php
The promise of asynchronous phpThe promise of asynchronous php
The promise of asynchronous php
 
Mojolicious
MojoliciousMojolicious
Mojolicious
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For Beginners
 
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applications
 
Drupal Development (Part 2)
Drupal Development (Part 2)Drupal Development (Part 2)
Drupal Development (Part 2)
 
PHP pod mikroskopom
PHP pod mikroskopomPHP pod mikroskopom
PHP pod mikroskopom
 
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい
 
ZeroMQ Is The Answer
ZeroMQ Is The AnswerZeroMQ Is The Answer
ZeroMQ Is The Answer
 

Dernier

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Dernier (20)

DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
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
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
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
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
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
 

Micropage in microtime using microframework

  • 1. 1
  • 2. 2
  • 6. name: Radosław Benkel nick: singles www: http://www.rbenkel.me twitter: @singlespl * * and I have nothing in common with http://www.singles.pl ;] 6
  • 7. SOMETIMES, FULL STACK FRAMEWORK IS AN OVERHEAD 7
  • 8. THIS IS WHY WE HAVE MICROFRAMEWORKS 8
  • 9. 9
  • 11. USUALLY, DOES SMALL AMOUT OF THINGS. 11
  • 12. USUALLY, DOES SMALL AMOUT OF THINGS. ROUTING 12
  • 13. USUALLY, DOES SMALL AMOUT OF THINGS. ROUTING HTTP CACHING 13
  • 14. USUALLY, DOES SMALL AMOUT OF THINGS. ROUTING HTTP CACHING TEMPLATES 14
  • 15. 15
  • 18. 640Koughttobe enoughforanyone. BTW.Probablyhedidn'tsaythat: HTTP://QUOTEINVESTIGATOR.COM/2011/09/08/640K-ENOUGH/ 18
  • 19. OK,OK,Iwantmeat! readas:Showmesomecodeplease 19
  • 20. Littleframework = littleamountofmeat 20
  • 22. BUT THERE ARE OTHERS: http://flightphp.com/ http://silex.sensiolabs.org/ http://www.limonade-php.net 22
  • 24. BASE ROUTING ?php require 'Slim/Slim.php'; $app = new Slim(); $app-get('/', function() { echo 'Hello World from base route.'; }); $app-run(); 24
  • 25. REQUIRED PARAM ?php require 'Slim/Slim.php'; $app = new Slim(); $app-get('/', function() { echo 'Hello World from base route.'; }); //param name is required $app-get('/hello_to/:name', function($name) { echo 'Hello World to ' . $name; }); $app-run(); 25
  • 26. OPTIONAL PARAM ?php require 'Slim/Slim.php'; $app = new Slim(); $app-get('/', function() { echo 'Hello World from base route.'; }); //when using optional params, you have to define default value for function param $app-get('/hello_to(/:name)', function($name = null) { if ($name === null) { $name = 'John Doe'; } echo 'Hello World to ' . $name; }); $app-run(); 26
  • 27. NAMED ROUTES ?php require 'Slim/Slim.php'; $app = new Slim(); $app-get('/', function() use ($app) { echo 'Hello World from base route.br'; $url = $app-urlFor('hello', array('name' = 'Jimmy')); //create link for route $link = sprintf('a href=%s%s/a', $url, $url); echo 'Oh, link to hello page for Jimmy is ' . $link; }); $app-get('/hello_to(/:name)', function($name = null) { if ($name === null) { $name = 'John Doe'; } echo 'Hello World to ' . $name; })-name('hello'); //using name for route $app-run(); 27
  • 28. ROUTE CONDITIONS ?php require 'Slim/Slim.php'; $app = new Slim(); $app-get('/', function() use ($app) { echo 'Hello World from base route.br'; $url = $app-urlFor('hello', array('name' = 'Jimmy')); $link = sprintf('a href=%s%s/a', $url, $url); echo 'Oh, link to hello page for Jimmy is ' . $link; }); $app-get('/hello_to(/:name)', function($name = null) { if ($name === null) { $name = 'John Doe'; } echo 'Hello World to ' . $name; })-name('hello') -conditions(array('name' = '[A-Za-z]+')); //use only letters as param 'name' $app-run(); 28
  • 29. REDIRECT ?php require 'Slim/Slim.php'; $app = new Slim(); /* ... */ $app-get('/hello_to(/:name)', function($name = null) { if ($name === null) { $name = 'John Doe'; } echo 'Hello World to ' . $name; })-name('hello') -conditions(array('name' = '[A-Za-z]+')); //redirect to default hello page $app-get('/redirect', function() use ($app) { $app-redirect($app-urlFor('hello')); }); $app-run(); 29
  • 30. REDIRECT WITH STATUS ?php require 'Slim/Slim.php'; $app = new Slim(); /* ... */ $app-get('/hello_to(/:name)', function($name = null) { if ($name === null) { $name = 'John Doe'; } echo 'Hello World to ' . $name; })-name('hello') -conditions(array('name' = '[A-Za-z]+')); //redirect to default hello page as 301, not 302 which is default $app-get('/redirect', function() use ($app) { $app-redirect($app-urlFor('hello'), 301); }); $app-run(); 30
  • 31. MIDDLEWARE ?php require 'Slim/Slim.php'; $app = new Slim(); $app-get('/', function() { //this will be executed before main callable echo Hello, I'm middleware br; }, function() use ($app) { echo 'Hello World from base route.br'; $url = $app-urlFor('hello', array('name' = 'Jimmy')); $link = sprintf('a href=%s%s/a', $url, $url); echo 'Oh, link to hello page for Jimmy is ' . $link; }); /* ... */ $app-run(); 31
  • 32. MIDDLEWARE ?php require 'Slim/Slim.php'; $app = new Slim(); $app-get('/', function() { //this will be executed before main callable echo Hello, I'm middleware br; }, function() { //this will be executed before main callable echo And I'm second middleware br; }, function() use ($app) { echo 'Hello World from base route.br'; $url = $app-urlFor('hello', array('name' = 'Jimmy')); $link = sprintf('a href=%s%s/a', $url, $url); echo 'Oh, link to hello page for Jimmy is ' . $link; }); /* ... */ $app-run(); 32
  • 33. MIDDLEWARE ?php require 'Slim/Slim.php'; $app = new Slim(); $app-get('/', function() { //this will be executed before main callable echo Hello, I'm middleware br; }, function() { //this will be executed before main callable echo And I'm second middleware br; }, function() use ($app) { echo 'Hello World from base route.br'; Andsoon-everythingbeforelastcallableis $url = $app-urlFor('hello', array('name' = 'Jimmy')); $link = sprintf('a href=%s%s/a', $url, $url); middleware echo 'Oh, link to hello page for Jimmy is ' . $link; }); /* ... */ $app-run(); 33
  • 34. VIEW ?php //file index.php require 'Slim/Slim.php'; $app = new Slim(); $app-get('/', function() use ($app) { $url = $app-urlFor('hello', array('name' = 'Jimmy')); //default path is __DIR__ . /templates return $app-render('view.php', compact('url')); }); /* ... */ $app-run(); Hello World from base route. br Oh, link to hello page for Jimmy is a href=?php echo $url??php echo $url?/a 34
  • 35. HTTP CACHE - ETAG ?php require 'Slim/Slim.php'; $app = new Slim(); /* ... */ $app-get('/hello_to(/:name)', function($name = null) use ($app) { if ($name === null) { $name = 'John Doe'; } //auto ETag based on some id - next request with the same name will return 304 Not Modified $app-etag($name); echo 'Hello World to ' . $name; })-name('hello') -conditions(array('name' = '[A-Za-z]+')); /* ... */ $app-run(); 35
  • 36. HTTP CACHE - TIME BASED ?php require 'Slim/Slim.php'; $app = new Slim(); /* ... */ $app-get('/hello_to(/:name)', function($name = null) use ($app) { if ($name === null) { $name = 'John Doe'; } $app-lastModified(1327305485); //cache based on time echo 'Hello World to ' . $name; })-name('hello') -conditions(array('name' = '[A-Za-z]+')); /* ... */ $app-run(); 36
  • 37. FLASH MESSAGE ?php require 'Slim/Slim.php'; $app = new Slim(); $app-get('/', function() use ($app) { $url = $app-urlFor('hello', array('name' = 'Jimmy')); return $app-render('view.php', compact('url')); }); //redirect to default page with flash message which will be displayed once $app-get('/redirect', function() use ($app) { $app-flash('info', You were redirected); $app-redirect($app-request()-getRootUri()); }); $app-run(); ?php echo $flash['info'] ? Hello World from base route. br Oh, link to hello page for Jimmy is a href=?php echo $url??php echo $url?/a 37
  • 38. CUSTOM 404 ?php require 'Slim/Slim.php'; $app = new Slim(); //define custom 404 page $app-notFound(function() { echo I'm custom 404; }); $app-get('/hello_to(/:name)', function($name = null) use ($app) { if ($name === null) { $name = 'John Doe'; } $possibleNames = array('Leonard', 'Sheldon', 'John Doe'); //when name not found, force 404 page if (array_search($name, $possibleNames) === false) { $app-notFound(); } echo 'Hello World to ' . $name; })-name('hello') -conditions(array('name' = '[A-Za-z]+')); $app-run(); 38
  • 39. CUSTOM 404 ?php require 'Slim/Slim.php'; $app = new Slim(); //define custom 404 page $app-notFound(function() { echo I'm custom 404; }); $app-get('/hello_to(/:name)', function($name = null) use ($app) { if ($name === null) { } Customerrorpage(500)alsopossible $name = 'John Doe'; $possibleNames = array('Leonard', 'Sheldon', 'John Doe'); //when name not found, force 404 page if (array_search($name, $possibleNames) === false) { $app-notFound(); } echo 'Hello World to ' . $name; })-name('hello') -conditions(array('name' = '[A-Za-z]+')); $app-run(); 39
  • 40. REST PATHS #1 ?php require 'Slim/Slim.php'; $app = new Slim(); //method name maps to HTTP method $app-get('/article'), function(/* ... */) {}); $app-post('/article'), function(/* ... */) {}); $app-get('/article/:id/'), function(/* ... */) {}); $app-put('/article/:id/'), function(/* ... */) {}); $app-delete('/article/:id/'), function(/* ... */) {}); 40
  • 41. REST PATHS #2 ?php require 'Slim/Slim.php'; $app = new Slim(); //same as previous one $app-map('/article'), function() use ($app) { if ($app-request()-isGet()) { /* ... */ } else if ($app-request()-isPost() { /* ... */ }) else { /* ... */ } })-via('GET', 'POST'); $app-map('/article/:id/'), function($id) use ($app) { //same as above })-via('GET', 'PUT', 'DELETE'); 41
  • 42. ALSO: ENCRYPTED SESSIONS AND COOKIES, APPLICATION MODES, CUSTOM TEMPLATES AND MORE... 42
  • 43. http://www.slimframework.com/ documentation/stable 43
  • 44. ButIcan'tusePHP5.3. Whatthen? 44
  • 45. PHP 5.2 ?php require 'Slim/Slim.php'; $app = new Slim(); function index() { global $app; echo 'Hello World from base route.br'; $url = $app-urlFor('hello', array('Jimmy')); $link = sprintf('a href=%s%s/a', $url, $url); echo 'Oh, link to hello page for Jimmy is ' . $link; } //last param must return true for is_callable call, so that it's valid $app-get('/', 'index'); /* ... */ $app-run(); 45
  • 46. PHP 5.2 ?php require 'Slim/Slim.php'; $app = new Slim(); function index() { global $app; echo 'Hello World from base route.br'; $url = $app-urlFor('hello', array('Jimmy')); $link = sprintf('a href=%s%s/a', $url, $url); echo 'Oh, link to hello page for Jimmy is ' . $link; } //last param must return true for is_callable call, so that it's valid Somebodysaid,that:everytime,whenyouuse $app-get('/', 'index'); /* ... */ global,unicorndies;) $app-run(); 46
  • 49. PHP 5.2 ?php class Controller { public static $app; public static function index() { echo 'Hello World from base route.br'; $url = self::$app-urlFor('hello', array('Jimmy')); $link = sprintf('a href=%s%s/a', $url, $url); echo 'Oh, link to hello page for Jimmy is ' . $link; } } require 'Slim/Slim.php'; $app = new Slim(); Controller::$app = $app; //last param must return true for is_callable call, so that it's also valid $app-get('/', array('Controller', 'index')); /* ... */ $app-run(); 49
  • 50. ButIMHOthisoneisthe bestsolution: 50
  • 51. PHP 5.2 ?php class Controller { protected $_app; public function __construct(Slim $app) { $this-_app = $app; } public function index() { echo 'Hello World from base route.br'; $url = $this-_app-urlFor('hello', array('Jimmy')); $link = sprintf('a href=%s%s/a', $url, $url); echo 'Oh, link to hello page for Jimmy is ' . $link; } } require 'Slim/Slim.php'; $app = new Slim(); $controller = new Controller($app); //last param must return true for is_callable call, so that it's also valid $app-get('/', array($controller, 'index')); /* ... */ $app-run(); 51
  • 52. SO, DO YOU REALLY NEED THAT ? Source: http://www.rungmasti.com/2011/05/swiss-army-knife/ 52
  • 53. 53