Your SlideShare is downloading. ×
Micropage in microtime using microframework
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Micropage in microtime using microframework

3,865

Published on

Why you don't always need full stack framework? Examples based on PHP's Slim Framework.

Why you don't always need full stack framework? Examples based on PHP's Slim Framework.

Published in: Technology
1 Comment
3 Likes
Statistics
Notes
No Downloads
Views
Total Views
3,865
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
54
Comments
1
Likes
3
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. 1
  • 2. 2
  • 3. PAGE 3
  • 4. PAGETIME 4
  • 5. PAGETIMEFRAMEWORK5
  • 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 HAVEMICROFRAMEWORKS 8
  • 9. 9
  • 10. ➜10
  • 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 CACHINGTEMPLATES 14
  • 15. 15
  • 16. Ihope,because... 16
  • 17. 640Koughttobeenoughforanyone. 17
  • 18. 640Koughttobe enoughforanyone. BTW.Probablyhedidntsaythat:HTTP://QUOTEINVESTIGATOR.COM/2011/09/08/640K-ENOUGH/ 18
  • 19. OK,OK,Iwantmeat! readas:Showmesomecodeplease 19
  • 20. Littleframework =littleamountofmeat 20
  • 21. ILL USEhttp://www.slimframework.com/ 21
  • 22. BUT THERE ARE OTHERS: http://flightphp.com/ http://silex.sensiolabs.org/ http://www.limonade-php.net 22
  • 23. SLIM EXAMPLES 23
  • 24. BASE ROUTING?phprequire Slim/Slim.php;$app = new Slim();$app-get(/, function() { echo Hello World from base route.;});$app-run(); 24
  • 25. REQUIRED PARAM?phprequire 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?phprequire 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 functionparam$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?phprequire 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?phprequire 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?phprequire 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?phprequire 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?phprequire Slim/Slim.php;$app = new Slim();$app-get(/, function() { //this will be executed before main callable echo Hello, Im 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?phprequire Slim/Slim.php;$app = new Slim();$app-get(/, function() { //this will be executed before main callable echo Hello, Im middleware br; }, function() { //this will be executed before main callable echo And Im 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?phprequire Slim/Slim.php;$app = new Slim();$app-get(/, function() { //this will be executed before main callable echo Hello, Im middleware br; }, function() { //this will be executed before main callable echo And Im 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.phprequire 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. brOh, link to hello page for Jimmy is a href=?php echo $url??php echo$url?/a 34
  • 35. HTTP CACHE - ETAG?phprequire 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 304Not 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?phprequire 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?phprequire 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. brOh, link to hello page for Jimmy is a href=?php echo $url??php echo $url?/a 37
  • 38. CUSTOM 404?phprequire Slim/Slim.php;$app = new Slim();//define custom 404 page$app-notFound(function() { echo Im 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?phprequire Slim/Slim.php;$app = new Slim();//define custom 404 page$app-notFound(function() { echo Im 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?phprequire 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?phprequire 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. ButIcantusePHP5.3. Whatthen? 44
  • 45. PHP 5.2?phprequire 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 its valid$app-get(/, index);/* ... */$app-run(); 45
  • 46. PHP 5.2?phprequire 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 its valid Somebodysaid,that:everytime,whenyouuse$app-get(/, index);/* ... */ global,unicorndies;)$app-run(); 46
  • 47. Source: http://tvtropes.org/pmwiki/pmwiki.php/Main/DeadUnicornTrope 47
  • 48. Sook,secondapproach: 48
  • 49. PHP 5.2?phpclass 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 its also valid$app-get(/, array(Controller, index));/* ... */$app-run(); 49
  • 50. ButIMHOthisoneisthe bestsolution: 50
  • 51. PHP 5.2?phpclass 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 its 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

×