SlideShare a Scribd company logo
1 of 53
Download to read offline
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

More Related Content

What's hot

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
 

What's hot (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
 

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

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

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
+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@
 

Recently uploaded (20)

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
 
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
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
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...
 
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
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
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
 
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
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
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
 
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
 
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
 
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
 
+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...
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 

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