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
 
PHP Tips & Tricks
PHP Tips & TricksPHP Tips & Tricks
PHP Tips & TricksRadek Benkel
 
Unit and Functional Testing with Symfony2
Unit and Functional Testing with Symfony2Unit and Functional Testing with Symfony2
Unit and Functional Testing with Symfony2Fabien Potencier
 
Current state-of-php
Current state-of-phpCurrent state-of-php
Current state-of-phpRichard McIntyre
 
RESTful web services
RESTful web servicesRESTful web services
RESTful web servicesTudor Constantin
 
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

Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
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 educationjfdjdjcjdnsjd
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
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
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
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
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
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
 
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
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
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
 

KĂŒrzlich hochgeladen (20)

Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
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
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
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
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
+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...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
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
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
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
 
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
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
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
 

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