Top Banner
1
53

Micropage in microtime using microframework

May 25, 2015

Download

Technology

Radek Benkel

Why you don't always need full stack framework? Examples based on PHP's Slim Framework.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Micropage in microtime using microframework

1

Page 2: Micropage in microtime using microframework

2

Page 3: Micropage in microtime using microframework

3

PAGE

Page 4: Micropage in microtime using microframework

4

PAGETIME

Page 5: Micropage in microtime using microframework

5

PAGETIMEFRAMEWORK

Page 6: Micropage in microtime using microframework

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 ;]

Page 7: Micropage in microtime using microframework

SOMETIMES, FULL STACK FRAMEWORK IS AN

OVERHEAD

7

Page 8: Micropage in microtime using microframework

THIS IS WHY WE HAVE MICROFRAMEWORKS

8

Page 9: Micropage in microtime using microframework

9

Page 10: Micropage in microtime using microframework

10

Page 11: Micropage in microtime using microframework

USUALLY, DOES SMALL AMOUT OF THINGS.

11

Page 12: Micropage in microtime using microframework

USUALLY, DOES SMALL AMOUT OF THINGS.

12

ROUTING

Page 13: Micropage in microtime using microframework

USUALLY, DOES SMALL AMOUT OF THINGS.

13

ROUTING

HTTP CACHING

Page 14: Micropage in microtime using microframework

USUALLY, DOES SMALL AMOUT OF THINGS.

14

ROUTING

TEMPLATES

HTTP CACHING

Page 15: Micropage in microtime using microframework

15

Page 16: Micropage in microtime using microframework

I����������� ������������������  hope,����������� ������������������  because...

16

Page 17: Micropage in microtime using microframework

17

����������� ������������������  "640K����������� ������������������  ought����������� ������������������  to����������� ������������������  be����������� ������������������  enough����������� ������������������  for����������� ������������������  anyone."

Page 18: Micropage in microtime using microframework

18

����������� ������������������  "640K����������� ������������������  ought����������� ������������������  to����������� ������������������  be����������� ������������������  enough����������� ������������������  for����������� ������������������  anyone."

BTW.����������� ������������������  Probably����������� ������������������  he����������� ������������������  didn't����������� ������������������  say����������� ������������������  that:HTTP://QUOTEINVESTIGATOR.COM/2011/09/08/640K-ENOUGH/

Page 19: Micropage in microtime using microframework

"OK,����������� ������������������  OK,����������� ������������������  I����������� ������������������  want����������� ������������������  meat!"

19

read����������� ������������������  as:����������� ������������������  "Show����������� ������������������  me����������� ������������������  some����������� ������������������  code����������� ������������������  please"

Page 20: Micropage in microtime using microframework

Little����������� ������������������  framework����������� ������������������  =����������� ������������������  

little����������� ������������������  amount����������� ������������������  of����������� ������������������  "meat"

20

Page 21: Micropage in microtime using microframework

I'LL USE

21

http://www.slimframework.com/

Page 22: Micropage in microtime using microframework

BUT THERE ARE OTHERS:

22

http://flightphp.com/

http://www.limonade-php.net

http://silex.sensiolabs.org/

Page 23: Micropage in microtime using microframework

SLIM EXAMPLES

23

Page 24: Micropage in microtime using microframework

24

<?php

require 'Slim/Slim.php';

$app = new Slim();

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

$app->run();

BASE ROUTING

Page 25: Micropage in microtime using microframework

25

<?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();

REQUIRED PARAM

Page 26: Micropage in microtime using microframework

26

<?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();

OPTIONAL PARAM

Page 27: Micropage in microtime using microframework

27

<?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();

NAMED ROUTES

Page 28: Micropage in microtime using microframework

28

<?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();

ROUTE CONDITIONS

Page 29: Micropage in microtime using microframework

29

<?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();

REDIRECT

Page 30: Micropage in microtime using microframework

30

<?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();

REDIRECT WITH STATUS

Page 31: Micropage in microtime using microframework

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();

Page 32: Micropage in microtime using microframework

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();

Page 33: Micropage in microtime using microframework

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>'; $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();

And����������� ������������������  so����������� ������������������  on����������� ������������������  -����������� ������������������  everything����������� ������������������  before����������� ������������������  last����������� ������������������  callable����������� ������������������  is����������� ������������������  middleware

Page 34: Micropage in microtime using microframework

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>

Page 35: Micropage in microtime using microframework

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();

Page 36: Micropage in microtime using microframework

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();

Page 37: Micropage in microtime using microframework

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>

Page 38: Micropage in microtime using microframework

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();

Page 39: Micropage in microtime using microframework

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) { $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();

Custom����������� ������������������  error����������� ������������������  page����������� ������������������  (500)����������� ������������������  also����������� ������������������  possible

Page 40: Micropage in microtime using microframework

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(/* ... */) {});

Page 41: Micropage in microtime using microframework

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');

Page 42: Micropage in microtime using microframework

ALSO: ENCRYPTED SESSIONS AND

COOKIES, APPLICATION MODES, CUSTOM TEMPLATES

AND MORE...

42

Page 44: Micropage in microtime using microframework

"But����������� ������������������  I����������� ������������������  can't����������� ������������������  use����������� ������������������  PHP����������� ������������������  5.3.����������� ������������������  What����������� ������������������  then?"

44

Page 45: Micropage in microtime using microframework

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();

Page 46: Micropage in microtime using microframework

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$app->get('/', 'index');

/* ... */

$app->run();

Somebody����������� ������������������  said,����������� ������������������  that:����������� ������������������  "everytime,����������� ������������������  when����������� ������������������  you����������� ������������������  use����������� ������������������  global,����������� ������������������  unicorn����������� ������������������  dies"����������� ������������������  ;)����������� ������������������  

Page 47: Micropage in microtime using microframework

47

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

Page 48: Micropage in microtime using microframework

So����������� ������������������  ok,����������� ������������������  second����������� ������������������  approach:

48

Page 49: Micropage in microtime using microframework

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();

Page 50: Micropage in microtime using microframework

But����������� ������������������  IMHO����������� ������������������  this����������� ������������������  one����������� ������������������  is����������� ������������������  the����������� ������������������  best����������� ������������������  solution:

50

Page 51: Micropage in microtime using microframework

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();

Page 52: Micropage in microtime using microframework

SO, DO YOU REALLY NEED THAT ?

52

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

Page 53: Micropage in microtime using microframework

53