[webconf 2015] 酒館的進退之道

[webconf 2015] 酒館的進退之道

3e6a21d5e1bc2d8371d17233338017c0?s=128

Cain Chen

May 15, 2015
Tweet

Transcript

  1. ꂋ긭涸鹎鷎⛓麥 栁ꆄ桄➃涸欴ㅷꬠㄐ

  2. ꡠ倴䧮 ˖ IJOBCMVFBLBꟐ⯕崡 ˖ */,琐㹐猰䪮Ⱏずⶾ鳵➃ ˖ #PVOUZ)VOUFS栁ꆄ桄➃$50 ˖ 8FC$POG闍䌌 ˖

    +4%$ 闍䌌
  3. 捀➊랃♶僽)BOB 䧮僽)JOB

  4. None
  5. 殹%SVQBM ♶ⱄ僽%SVQBM 欴ㅷꟚ涮涸噲ꣳ莅刈僳

  6. ꂋ긭涸㔮㞯 ˖ 饥騟♶㼭䗱剚驫ⵌ垸穉 ˖ 馄ⴀ%SVQBM姻䌢⢪欽眕毑 ˖ 禺窡剪⹡馄ⴀ頾蚛 ˖ Ꟛ涮➃㆞く筝 ˖

    儘玑搂岁焷ⴗ䱍䳣
  7. #講個秘訣 獎⾦金獵⼈人倘若關閉快取,⼀一個⾴頁⾯面需要 25 秒的伺服器回應時間。

  8. Ꟛ涮植屣 ˖ Ꟛ涮垸䒭⛰䧭⟃♳⢪欽垸穉䭨廔 ˖ 䪮遯⫈ ˖ %SVQBM⫦ⶦ♴Ⱇデ⸆腋 ˖ (JU搂剤佪䥰欽 ˖

    䖕糵䱺⸂蕲䩛 ˖ ⫦ꬒ㢵ꅾ䘰《⮛⻋
  9. Ꟛ涮植屣

  10. 刈僳涸湡垦 ˖ 宠倞宠䘰宠隶 ˖ 鏤鎙魨ꤴ㔾㕎 ˖ ⢪欽罏㔐꺈 ˖ 涮䟝莅㥼䟝 ˖

    娦脌ず
  11. 殹%SVQBM 麂♳1IBMDPO1)1 欴ㅷ銴涸僽鱲㘗鼩僽侮䕎

  12. 鱲㘗鼩僽侮䕎

  13. 㖈鸏⛓⵹ ˖ 3VCZPO3BJMT ˖ /PEF+4 ˖ $PEF*HOJUFS ˖ -BSBWFM ˖

    1IBMDPO1)1
  14. 捀➊랃鼩僽1)1 ˖ ➃⸂䧭劥
 ♶欽ⱄ㷸♧妄倞涸匌銯 ˖ Ꟛ涮ꨈ䏞
 ꨆꟚ擿䜫涸橇㞯剚♶剚僽〥♧㜥䟅㣆 ˖ 儘玑䱾ⵖ
 Ꟛ涮⸆腋儘玑僽や《对倴䧮(PPHMF♧♴

    ˖ 佐俈䪮遯
 䪾侕衆♧㖒涸垸穉书饱⢵ #講個秘訣 ⽐比起 PHP 我還更喜歡 NodeJS(欸
  15. 1IBMDPO1)1 ˖ 㥪贖 ˖ 饱䩛䒭湱㼩㺂僒 ˖ ♶欽ⱄ禾穡倴垸穉 ˖ 鸠䏞䘰 ˖

    〳⟃涮㾝荈䊹涸&YUFOTJPO ˖ 緄럊 ˖ Y呍䗱ꐫ铐瘞倴潥㶩䷬韍 ˖ ⩑晋剚麂ⵌꟚ涮➃㆞UZQP ˖ ⛓⵹搂㸤侮⾲㨥焺〳⿮罌
  16. 侮䕎⵹ ˖ %SVQBM⛓倴欴ㅷ涸䒧畮 ˖ 銼㻨㣖㢵垸穉 ˖ 垸穉湱⣜琎稡麕倴㓂ꅾ ˖ ⵹畮搂岁剤佪⮛⻋ ˖

    ⸆腋꨾宠馄馊荈魨⸆腋眕毑 ˖ 垸穉⛓꟦搂岁剤佪ꅾ欽 ˖ ꬌ䘰《♶〳 #講個秘訣 這是個模組蓋來蓋去的戰爭。
  17. 侮䕎⵹ ˖ 瀷䱈ꅾ箻㼩倴栁ꆄ桄➃涸䕧갠 ˖ ⸆腋垸穉䗳갭ꅾ㻨 ˖ 须俲穡圓꨾ꅾ倞㹁纏 ˖ ➃㆞鎯箻 ˖

    ꅾ倞㹁纏Ꟛ涮莅庠鑑 ˖ 㼬Ⰵ莅佖鹎Ꟛ涮崩玑莅䊨Ⱘ
  18. 侮䕎⵹ ˖ 1IBMDPO1)1㼩倴栁ꆄ桄➃涸湡涸 ˖ 㔐ⵌ♧⦐1)1FS罜♶僽♧湬佖垸穉 ˖ 濼麥FYUFOET莅JNQMFNFOUT涸䊴ⴽ ˖ 濼麥BCTUSBDUDMBTT僽➊랃 ˖

    濼麥荈䊹㻨涸〭⨞1)1 ˖ 㣔♴娀⸆㈔䘰♶灶
  19. 侮䕎䖕

  20. 侮䕎䖕 ˖ %SVQBM ˖ 垸穉⸆腋 ˖ ⵹畮 ˖ 须俲穡圓 ˖

    1IBMDPO1)1 ˖ 鼩僽垸穉 ˖ 7PMU5FNQMBUF ˖ 1)2-03.0%. ˖ 6OJU5FTU ˖ %* #講個秘訣 http://php-di.org
  21. 侮䕎䖕 ˖ ꅾ倞钢陏荈䊹涸欴ㅷ ˖ ⵹䖕畮㸤侮ⴕꨆ ˖ .7$ ˖ ⵹畮娝⡙ ˖

    荈䊹涸庠鑑荈䊹⨞
  22. 侮䕎䖕 ˖ "$-T ˖ 荈遤䒊用奚ꣳ幢㋲ ˖ 3PVUFS ˖ ꧌ざ䨾剤䥰欽玑䒭Ⰵ〡 ˖

    .PEVMFT ˖ 䥰欽玑䒭㼠㿂垸穉 ˖ $POUSPMMFST$PNQPOFOUT.PEFMT1MVHJOT7JFXT ˖ 䥰欽玑䒭玑䒭焺须俲䏨⟃⿻Ⱖ➮䥰欽 ˖ 5BTLT ˖ $-*莅荈⹛⻋䊨Ⱘ
  23. 1IBMDPO1)1 ⴲ㨥⻋ 殹鎙殥馜♶♳隶⻋ 隶⻋侰♶麕㹐䨩涸♧鸒ꨶ鑨

  24. 奚ꣳ䱾盘 ˖ "$- ˖ 㸞䱖錬蒀
 ˖ 颭✮须彂
 ˖ 鏤㹁奚ꣳ
 $acl->addRole('Guests');

    $acl->addResource('Index', 'index'); $acl->allow('Guests', 'Index', 'index');
  25. 䥰欽玑䒭 ˖ ⢪欽registerModules鏽ⱁ㢵穉䥰欽玑䒭 /* index.php */ $application = new \Phalcon\Mvc\Application($di);

    $application->setEventsManager($eventsManager); $application->registerModules(array( 'bhunter' => array( 'className' => 'Bhunter\Module', 'path' => '/apps/bhunter/Module.php' ) )); echo $application->handle()->getContent();
  26. 䥰欽玑䒭 ˖ 鏽ⱁ荈⹛隡《莅剪⹡ /* Module.php */ public function registerAutoloaders($di) {

    $loader = new Loader(); $loader->registerNamespaces(array(…)); $loader->registerDirs(array(…)); $loader->register(); } public function registerServices($di) { $di->set('dispatcher', function() use ($di) { } }
  27. 䥰欽玑䒭 ˖ 剪⹡涸ぐⴽ鏤㹁 /* Module.php */ public function registerServices($di) {

    $di->set('view', function() { $view = new View(); $view->setViewsDir('../apps/backend/views/'); return $view; }); }
  28. 䥰欽玑䒭 ˖ 䱾ⵖ㐼
 
 
 
 
 
 ˖ 垺晝

    /* index.volt */ {% extends "layout" %} {% block content %} <h1>Hello World</h1> {% endblock %} /* IndexController.php */ class IndexController extends \Phalcon\Mvc\Controller { }
  29. 䥰欽玑䒭 ˖ 䥰欽玑䒭剓穅卺圓 apps/ Bhunter/ Controllers/ Models/ Plugins/ Views/ Tasks/

    Module.php public/ index.php
  30. 刿㢵涸䥰欽 ˖ %* ˖ &WFOUT.BOBHFNFOU ˖ "TTFUT.BOBHFNFOU ˖ 7JFX7JFX)FMQFST ˖

    2VFVFJOH ˖ $PNNBOE-JOF"QQMJDBUJPOT ˖ %FWFMPQFS5PPMT ˖ 1SPMJOH
  31. 1IBMDPO1)1 遥欰涸㉏겗 䱺「倞涸卺圓 㽠䖤鸮䌟䱺「倞涸㉏겗

  32. 遥欰㉏겗 ˖ Ⱏ欽涸穉⟝⡦贖⿡ ˖ 僽や䱰欽痧♲倰㤛⟝ ˖ ⵹畮莅垺晝䒸乿涸湱㺂 ˖ 㹐醢⻋垺晝㥶⡦䵩ꂂ ˖

    ⵹䖕畮ⴕꨆ涸庠鑑 ˖ 䘰《⡦贖⿡ ˖ .PEFM鿪僽佞须俲涸㖒倰 ˖ 蠝腋%*鸤㽠✫➊랃
  33. 遥欰㉏겗 ˖ Ⱏ欽涸穉⟝⡦贖⿡ ˖ 䬄韍⻋ ˖ 须俲穡圓ⴕꨆ ˖ 齅鱀ⴕꨆ

  34. class CommentComponent extends Phalcon\Mvc\User \Component
 {
 class CommentPlugin extends Phalcon\Mvc\User\Plugin


    {
 } class CommentModule extends Phalcon\Mvc\User\Module
 {
 }
  35. 遥欰㉏겗 ˖ 僽や䱰欽痧♲倰㤛⟝ ˖ ⢪欽$PNQPTFS ˖ 鼚⯝ꅾ䏞⣜飅 ˖ 犝姺佖㻨傂剤㤛⟝ ˖

    荈䊹涸㤛⟝荈䊹㻨 ˖ 僈焷㹁纏⢪欽橇㞯
  36. 遥欰㉏겗 ˖ ⵹畮莅垺晝䒸乿涸湱㺂 ˖ 莅5XJH湱⡂ ˖ 7PMU莅"OHVMBS+4涸湱㺂 ˖ 荈䊹涸'JMUFS荈䊹㻨 ˖

    荈䊹涸'VODUJPO荈䊹⨞ ˖ ♶ⴀ植<?php㖈垺晝⚥ #講個秘訣 Volt 有個神奇的 bug。
  37. /* IndexController.php */ $this->view->setVar('name', 'Hina'); $this->view->setVar('friends', ['AJ', 'Zac', 'Hana']); /*

    index.volt */ <h1>My Name is {{ name }}.</h1> <p>Here is my Friends, {% for name in friends %} {{ name }}{% if loop.last is false %},{% endif %} {% endfor %}. </p> <h3>I’m {{ name }}.</h3>
  38. 㼩♶饱䧮僽)JOB

  39. /* compiled volt */ <h1>My Name is <?php echo $name;

    ?>.</h1> <p>Here is my Friends, <?php … ?> <?php foreach ($v180913826011169360571iterator as $name) { ?> <?php echo $name; ?> <?php } … ?>. </p> <h3>I’m <?php echo $name; ?>.</h3> $name 麒㖒Ꟛ蔅
  40. 遥欰㉏겗 ˖ 㹐醢⻋垺晝㥶⡦䵩ꂂ ˖ 垺晝⛳〳⟃剤⯋⟝ ˖ 栬用⢪欽layout ˖ ⯋⟝⢪欽view->pick(…) ˖

    ⴕꨆꬆ䢀墂呪 ˖ 歋Components㋲栬䱾ⵖ
  41. class CommentController extends Phalcon\Mvc \Coontroller
 {
 // DO something.
 


    $this->assets->addJs('js/comment.js');
 $this->view->pick('comment/comments');
 }
  42. 遥欰㉏겗 ˖ ⵹䖕畮ⴕꨆ涸庠鑑 ˖ 1)16OJU5FTU ˖ ,BSNB.PDIB5FTU

  43. 剤Ⱄ꣚ㆥ

  44. 遥欰㉏겗 ˖ 䘰《⡦贖⿡ ˖ 7BSOJTI ˖ "1$ ˖ 9$BDIF ˖

    1IBMDPO1)1佅䴂.VMUJQMF$BDIF ˖ .FNDBDIFE ˖ .POHP%# ˖ 'JMF
  45. $cache = new Multiple(array( new Backend\Libmemcached(new Frontend\Data(array( 'lifetime' => 3600

    )), array( 'servers' => array(array( 'host' => 'localhost', 'port' => 11211, 'weight' => 1, )), 'client' => array( \Memcached::OPT_HASH => \Memcached::HASH_MD5, \Memcached::OPT_PREFIX_KEY => 'prefix.', ), 'lifetime' => 3600, )), new Backend\File(new Frontend\Data(array( 'lifetime' => 86400 )), array( 'prefix' => 'prefix.', 'cacheDir' => '../cache/' )) ));
  46. 遥欰㉏겗 ˖ .PEFM鿪僽佞须俲涸㖒倰㌨ ˖ 须俲䏨贖椚 ˖ 齅鱀贖椚 ˖ ⵹畮须俲贖椚

  47. class Comment extends Phalcon\Mvc\Model { public $id; public $title; public

    $content; /** * Independent Column Mapping. */ public function columnMap() { return array( 'id' => 'id', 'title' => 'title', 'content' => 'content', ); } }
  48. 遥欰㉏겗 ˖ 蠝腋%*鸤㽠✫➊랃 ˖ ꣚♶㽠㥪問問 ˖ ⵌ贖鿪僽%* ˖ %*䌟⢵涸⤑ⵄ溫涸⤑ⵄ㌨

  49. class Comment extends Phalcon\Mvc\Model { public $id; public $title; public

    $content; public function getComments() { $di = $this->getDI(); $manager = new Phalcon\Mvc\Model\Manager(); $manager->setDI($di); $builder = $manager->createBuilder(); // Do something. $paginator = new Phalcon\Paginator\Adapter \QueryBuilder(array('builder' => $builder)); $comments = $paginator->getPaginate(); // Code Bad Smell $di->session->set('COMMENTS', $comments); }
  50. /* CommentComponent.php */ class CommentComponent extends Phalcon\Mvc\User \Component { public

    function getComments() { } } /* CommentController.php */ $di->set('elements', function(){ return new CommentComponent(); }); <!—— Comment.volt ——> {{ elements.getComments() }}
  51. 1IBMDPO1)1 鍑对涸㉏겗 鸏♶僽CVH 僽GFBUVSF

  52. 鍑对㉏겗 ˖ 垸穉♶ⱄ幋差♶幢 ˖ 葻㥪涸庠鑑 ˖ ꫙崞涸崩玑䱾ⵖ ˖ 刿㺂僒涸ꤑꐫ ˖

    Ⳗ翹Ꟛ涮Ⱏ陏 ˖ 涮植㉏겗 ˖ 佪腋
  53. 鍑对㉏겗 ˖ 垸穉♶ⱄ幋差♶幢 apps/ Bhunter/ Controllers/ Components/ CompetitionsGridComponent.php CompetitionsController.php Views/

    Components/ CompetitionsGridComponent.php Models/ Competitions.php
  54. ˖ 庠鑑 鍑对㉏겗

  55. 鍑对㉏겗 ˖ ꫙崞涸崩玑䱾ⵖ /* EditorController.php */ class EditorController extends \Phalcon\Mvc\Controller

    { public function indexAction() { $component = implode('\', array( __NAMESPACE__, 'Components', 'EditorComponent' )); $component = new $component; #講個秘訣 這是個偷懶的範例,好孩⼦子不要學。
  56. 鍑对㉏겗 ˖ 刿㺂僒涸ꤑꐫ

  57. 鍑对㉏겗 ˖ Ⳗ翹Ꟛ涮Ⱏ陏 ˖ ⼿锅⠒⠶⛓꟦擿䜫涸갪湡 ˖ ⴕꂂぐ荈涸䓽갪Ꟛ涮 ˖ 嫦♧妄⸂㸤䧭♧갪湡垦 ˖

    0O4DIFEVMF
  58. 鍑对㉏겗 ˖ 涮植㉏겗 ˖ $PEF3FWJFX ˖ 1BJS1SPHSBNNJOH

  59. $PEF3FWJFX

  60. 1BJS1SPHSBNNJOH

  61. None
  62. 鍑对㉏겗 ˖ 佪腋 # ab -n 10000 -c 100 #

    ab -n 1000 -c 100
  63. 鍑对㉏겗 ˖ 鸏僽湡⵹禺窡剓䢩涸.PEFM

  64. ꂋ긭涸&YUFOTJPO 1IBMDPO1)1莅;FQIJS 傂搭♶㣁䘰 齡⺫䧭&YUFOTJPO䧴鏪剚䖎䘰

  65. ;FIQJS ˖ 1IBMDPO1)1㕰꥙䲿⣘ ˖ IUUQ[FQIJSMBOHDPN ˖ ♶♧㹁銴剚$ ˖ ⡎僽饱焺剚㻨1)1 ˖

    ♶⥃阮剚嫲鯱䘰 ˖ 姻䒭橇㞯鼩♶琽㹁
  66. ꂋ긭涸劢⢵ ꂋ긭Ꟛ彂 荈䊹涸ꂋ긭荈䊹Ꟛ

  67. 0QFO4PVSDF

  68. 5IBOLZPV 䧮⦛僽栁ꆄ桄➃ 䧮⦛僽Ꟛ䖃㣆䟝涸湬麨鮦