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

PDF
[PL] Jak nie zostać "programistą" PHP?
PDF
PhpBB meets Symfony2
PDF
Introducing Assetic (NYPHP)
PDF
Frameworks da nova Era PHP FuelPHP
PPTX
Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
PDF
jQuery: Events, Animation, Ajax
PDF
Mojolicious
KEY
Lithium Best
[PL] Jak nie zostać "programistą" PHP?
PhpBB meets Symfony2
Introducing Assetic (NYPHP)
Frameworks da nova Era PHP FuelPHP
Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
jQuery: Events, Animation, Ajax
Mojolicious
Lithium Best

What's hot (20)

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

Similar to Micropage in microtime using microframework (20)

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

Recently uploaded (20)

PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Mushroom cultivation and it's methods.pdf
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
A Presentation on Touch Screen Technology
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
August Patch Tuesday
PDF
1 - Historical Antecedents, Social Consideration.pdf
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
DP Operators-handbook-extract for the Mautical Institute
PDF
Hybrid model detection and classification of lung cancer
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Hindi spoken digit analysis for native and non-native speakers
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Approach and Philosophy of On baking technology
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PPTX
Tartificialntelligence_presentation.pptx
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PPTX
OMC Textile Division Presentation 2021.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
Mushroom cultivation and it's methods.pdf
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
A Presentation on Touch Screen Technology
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
August Patch Tuesday
1 - Historical Antecedents, Social Consideration.pdf
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
Programs and apps: productivity, graphics, security and other tools
DP Operators-handbook-extract for the Mautical Institute
Hybrid model detection and classification of lung cancer
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Hindi spoken digit analysis for native and non-native speakers
Unlocking AI with Model Context Protocol (MCP)
Approach and Philosophy of On baking technology
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
Tartificialntelligence_presentation.pptx
gpt5_lecture_notes_comprehensive_20250812015547.pdf
OMC Textile Division Presentation 2021.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
  • 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