Upgrade to Pro — share decks privately, control downloads, hide ads and more …

[webconf 2015] 酒館的進退之道

[webconf 2015] 酒館的進退之道

Cain Chen

May 15, 2015
Tweet

More Decks by Cain Chen

Other Decks in Technology

Transcript

  1. 1IBMDPO1)1 ˖ 㥪贖 ˖ 饱䩛䒭湱㼩㺂僒 ˖ ♶欽ⱄ禾穡倴垸穉 ˖ 鸠䏞䘰 ˖

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

    ⸆腋꨾宠馄馊荈魨⸆腋眕毑 ˖ 垸穉⛓꟦搂岁剤佪ꅾ欽 ˖ ꬌ䘰《♶〳 #講個秘訣 這是個模組蓋來蓋去的戰爭。
  3. 侮䕎䖕 ˖ %SVQBM ˖ 垸穉⸆腋 ˖ ⵹畮 ˖ 须俲穡圓 ˖

    1IBMDPO1)1 ˖ 鼩僽垸穉 ˖ 7PMU5FNQMBUF ˖ 1)2-03.0%. ˖ 6OJU5FTU ˖ %* #講個秘訣 http://php-di.org
  4. 侮䕎䖕 ˖ "$-T ˖ 荈遤䒊用奚ꣳ幢㋲ ˖ 3PVUFS ˖ ꧌ざ䨾剤䥰欽玑䒭Ⰵ〡 ˖

    .PEVMFT ˖ 䥰欽玑䒭㼠㿂垸穉 ˖ $POUSPMMFST$PNQPOFOUT.PEFMT1MVHJOT7JFXT ˖ 䥰欽玑䒭玑䒭焺须俲䏨⟃⿻Ⱖ➮䥰欽 ˖ 5BTLT ˖ $-*莅荈⹛⻋䊨Ⱘ
  5. 奚ꣳ䱾盘 ˖ "$- ˖ 㸞䱖錬蒀
 ˖ 颭✮须彂
 ˖ 鏤㹁奚ꣳ
 $acl->addRole('Guests');

    $acl->addResource('Index', 'index'); $acl->allow('Guests', 'Index', 'index');
  6. 䥰欽玑䒭 ˖ ⢪欽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();
  7. 䥰欽玑䒭 ˖ 鏽ⱁ荈⹛隡《莅剪⹡ /* 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) { } }
  8. 䥰欽玑䒭 ˖ 剪⹡涸ぐⴽ鏤㹁 /* Module.php */ public function registerServices($di) {

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

    /* index.volt */ {% extends "layout" %} {% block content %} <h1>Hello World</h1> {% endblock %} /* IndexController.php */ class IndexController extends \Phalcon\Mvc\Controller { }
  10. 刿㢵涸䥰欽 ˖ %* ˖ &WFOUT.BOBHFNFOU ˖ "TTFUT.BOBHFNFOU ˖ 7JFX7JFX)FMQFST ˖

    2VFVFJOH ˖ $PNNBOE-JOF"QQMJDBUJPOT ˖ %FWFMPQFS5PPMT ˖ 1SPMJOH
  11. 遥欰㉏겗 ˖ Ⱏ欽涸穉⟝⡦贖⿡ ˖ 僽や䱰欽痧♲倰㤛⟝ ˖ ⵹畮莅垺晝䒸乿涸湱㺂 ˖ 㹐醢⻋垺晝㥶⡦䵩ꂂ ˖

    ⵹䖕畮ⴕꨆ涸庠鑑 ˖ 䘰《⡦贖⿡ ˖ .PEFM鿪僽佞须俲涸㖒倰 ˖ 蠝腋%*鸤㽠✫➊랃
  12. 遥欰㉏겗 ˖ ⵹畮莅垺晝䒸乿涸湱㺂 ˖ 莅5XJH湱⡂ ˖ 7PMU莅"OHVMBS+4涸湱㺂 ˖ 荈䊹涸'JMUFS荈䊹㻨 ˖

    荈䊹涸'VODUJPO荈䊹⨞ ˖ ♶ⴀ植<?php㖈垺晝⚥ #講個秘訣 Volt 有個神奇的 bug。
  13. /* 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>
  14. /* 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 麒㖒Ꟛ蔅
  15. class CommentController extends Phalcon\Mvc \Coontroller
 {
 // DO something.
 


    $this->assets->addJs('js/comment.js');
 $this->view->pick('comment/comments');
 }
  16. 遥欰㉏겗 ˖ 䘰《⡦贖⿡ ˖ 7BSOJTI ˖ "1$ ˖ 9$BDIF ˖

    1IBMDPO1)1佅䴂.VMUJQMF$BDIF ˖ .FNDBDIFE ˖ .POHP%# ˖ 'JMF
  17. $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/' )) ));
  18. 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', ); } }
  19. 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); }
  20. /* 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() }}
  21. 鍑对㉏겗 ˖ ꫙崞涸崩玑䱾ⵖ /* EditorController.php */ class EditorController extends \Phalcon\Mvc\Controller

    { public function indexAction() { $component = implode('\', array( __NAMESPACE__, 'Components', 'EditorComponent' )); $component = new $component; #講個秘訣 這是個偷懶的範例,好孩⼦子不要學。