CakePHPã®ã¬ã³ããªã³ã°å¦ç(render)
CakePHP1.2ãviewã®ã¬ã³ããªã³ã°å¦çé¨åã«ã¤ãã¦ã®ã¡ã¢ã
Controller::render()
viewã解éãã¦ãçµæãoutputããããã£ã«æååã¨ãã¦æ ¼ç´ããã¡ã½ããã
æ»ãå¤ã¨ãã¦ã¬ã³ããªã³ã°çµæãè¿ããå®éã®åºåã¾ã§ã¯ãããªãã
è¤æ°åå¼ã³åºãã¨ãåºåå
容ã¯ä¸æ¸ããããã®ã§ã¯ãªã追å ããã¦ããã
ä¸åº¦renderãå¼ã³åºãã¨autoRenderã¯falseã¨ãªãã
<?php class Controller extends Object { function render($action = null, $layout = null, $file = null) { // ç¥ï¼å¦çæºåï¼ $this->autoRender = false; // Viewã®renderãå®éã®å¦çãè¡ã // çµæã¯è¿½å ããã¦ãã $this->output .= $View->render($action, $layout, $file); // ã¬ã³ããªã³ã°çµæãè¿ã return $this->output; } }
Dispatcher::_invoke()
ã³ã³ããã¼ã©ã®ã¢ã¯ã·ã§ã³ãå®è¡ãã¦ãã¬ã³ããªã³ã°çµæãåºåããã¡ã½ããã
beforeFilterãafterFilterãªã©ãããã§å¼ã°ããã
autoRenderãtrueã®å ´åã«ã¯ãrenderã¡ã½ãããå®è¡ãã¦æ»ãå¤ãoutputããããã£ã«æ ¼ç´ãããï¼è¿½å ã§ã¯ãªãä¸æ¸ãããã®ã«æ³¨æï¼
outputããããã£ã空ã®å ´åã«ã¯ãactionã®æ»ãå¤ãoutputããããã£ã«æ ¼ç´ããã
<?php class Dispatcher extends Object { function _invoke(&$controller, $params) { $controller->constructClasses(); $controller->Component->initialize($controller); $controller->beforeFilter(); $controller->Component->startup($controller); // ç¥ // ã¢ã¯ã·ã§ã³ãå®è¡ $output = $controller->dispatchMethod($params['action'], $params['pass']); if ($controller->autoRender) { // autoRenderãtrueã®å ´åãrenderã®æ»ãå¤ãåºåå 容ã¨ãã $controller->output = $controller->render(); } elseif (empty($controller->output)) { // outputããããã£ã空ã®å ´åãã¢ã¯ã·ã§ã³ã®æ»ãå¤ãåºåå 容ã¨ãã $controller->output = $output; } $controller->Component->shutdown($controller); $controller->afterFilter(); // ç¥ // åºåãã echo($controller->output); } }
ã¬ã³ããªã³ã°ï¼ã¤ã®æ¹æ³
- autoRenderãtrueã«ãã¦ãã
- æ示çã«renderãå¼ã¶
- autoRenderãfalseã«ãã¦ãreturnã§åºåå 容ãè¿ã(outputã空ã®ã¨ã)
- autoRenderãfalseã«ãã¦ãoutputã«ç´æ¥åºåå 容ãæ ¼ç´ãã
<?php class FooController extends AppController { function action1() { $this->autoRender = true; // çç¥å¯ } function action2() { $this->render('foo'); } function action3() { $this->autoRender = false; $this->output = null; // çç¥å¯ return 'åºåãããå 容'; } function action4() { $this->autoRender = false; $this->output = 'åºåãããå 容'; } }
renderã®çµæãæååã¨ãã¦åå¾ãã
renderã®çµæãæååã¨ãã¦åå¾ããããåºåã¯ããªãæ¹æ³ã
renderå¼ã³åºãå¾outputã空ã«ãããå¿
è¦ã«å¿ãã¦autoRenderãtrueã«æ»ãã
<?php function action() { $output = $this->render('foo'); // åºåå 容ãæååã§åå¾ $this->output = null; // outputã空ã«ãã $this->autoRender = true; // trueã«æ»ã }
Object::requestAction()ã®optionã«returnãè¨å®ããã¨ã
ç¹å®ã®actionã®ã¬ã³ããªã³ã°çµæãåå¾ãããã¨ãã§ããã
<?php function action() { $output = $this->requestAction('/bar/baz', array('return')); }