SlideShare ist ein Scribd-Unternehmen logo
1 von 53
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

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 Fieldfabulouspsychop39
 
Mojolicious. Веб в коробке!
Mojolicious. Веб в коробке!Mojolicious. Веб в коробке!
Mojolicious. Веб в коробке!Anatoly Sharifulin
 
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'tCosimo Streppone
 
Unit and Functional Testing with Symfony2
Unit and Functional Testing with Symfony2Unit and Functional Testing with Symfony2
Unit and Functional Testing with Symfony2Fabien Potencier
 
Mojolicious - A new hope
Mojolicious - A new hopeMojolicious - A new hope
Mojolicious - A new hopeMarcus Ramberg
 
Plugin jQuery, Design Patterns
Plugin jQuery, Design PatternsPlugin jQuery, Design Patterns
Plugin jQuery, Design PatternsRobert Casanova
 
symfony on action - WebTech 207
symfony on action - WebTech 207symfony on action - WebTech 207
symfony on action - WebTech 207patter
 
jQuery Plugin Creation
jQuery Plugin CreationjQuery Plugin Creation
jQuery Plugin Creationbenalman
 
Interface de Voz con Rails
Interface de Voz con RailsInterface de Voz con Rails
Interface de Voz con RailsSvet Ivantchev
 
Silex: From nothing to an API
Silex: From nothing to an APISilex: From nothing to an API
Silex: From nothing to an APIchrisdkemper
 
The promise of asynchronous php
The promise of asynchronous phpThe promise of asynchronous php
The promise of asynchronous phpWim Godden
 
PHP an intro -1
PHP an intro -1PHP an intro -1
PHP an intro -1Kanchilug
 
Building Your First Widget
Building Your First WidgetBuilding Your First Widget
Building Your First WidgetChris Wilcoxson
 
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)Dennis Knochenwefel
 
Desymfony 2011 - Habemus Bundles
Desymfony 2011 - Habemus BundlesDesymfony 2011 - Habemus Bundles
Desymfony 2011 - Habemus BundlesAlbert Jessurum
 
My app is secure... I think
My app is secure... I thinkMy app is secure... I think
My app is secure... I thinkWim Godden
 
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, FishBrainCodemotion Tel Aviv
 

Was ist angesagt? (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
 

Ähnlich wie Micropage in microtime using microframework

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!Kacper Gunia
 
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 CracowKacper Gunia
 
Modern Web Development with Perl
Modern Web Development with PerlModern Web Development with Perl
Modern Web Development with PerlDave Cross
 
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 middlewareKuan Yen Heng
 
Aura Project for PHP
Aura Project for PHPAura Project for PHP
Aura Project for PHPHari K T
 
Nashvile Symfony Routes Presentation
Nashvile Symfony Routes PresentationNashvile Symfony Routes Presentation
Nashvile Symfony Routes PresentationBrent Shaffer
 
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applicationselliando dias
 
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 frameworksElton Minetto
 
Symfony components in the wild, PHPNW12
Symfony components in the wild, PHPNW12Symfony components in the wild, PHPNW12
Symfony components in the wild, PHPNW12Jakub Zalas
 
The promise of asynchronous php
The promise of asynchronous phpThe promise of asynchronous php
The promise of asynchronous phpWim Godden
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For BeginnersJonathan Wage
 
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applicationselliando dias
 
Drupal Development (Part 2)
Drupal Development (Part 2)Drupal Development (Part 2)
Drupal Development (Part 2)Jeff Eaton
 
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐいHisateru Tanaka
 
ZeroMQ Is The Answer
ZeroMQ Is The AnswerZeroMQ Is The Answer
ZeroMQ Is The AnswerIan Barber
 

Ähnlich wie 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
 

Kürzlich hochgeladen

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 

Kürzlich hochgeladen (20)

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 

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