ãã¼ã¸ã®é·ç§»ã管çããTransitionã³ã³ãã¼ãã³ã
Transitionã³ã³ãã¼ãã³ã1.0ããªãªã¼ã¹ãã¾ããã
http://d.hatena.ne.jp/hiromi2424/20100617/1276757385
ã½ã¼ã¹ã¯ãã¡ã
http://github.com/hiromi2424/TransitionComponent
ç»é¢å³ä¸ããããDownload Sourcesããã¯ãªãã¯ããã¨ãã¢ã¼ã«ã¤ãããã¦ã³ãã¼ãã§ãã¾ãã
pull request ãå¾ ã¡ãã¦ãã¾ã:)
ã¤ã³ãããã¯ã·ã§ã³
è¤æ°ãã¼ã¸ã®å ¥åç»é¢ãçããã©ãããå¦çããã¦ãã¾ããï¼
åãã¢ã¯ã·ã§ã³å
ã§é·ç§»ããå ´åããã¼ã¿ãåå¨ããããã§ãã¯ãã¦ã©ãã®ä½ç½®ã«ãããå¤æãã¦ã»ã»ã»ã¨ãè²ã
ã¨é¢åã§ãããã
ä»ã®ã¢ã¯ã·ã§ã³ã«åå²ãã¦ãä¼¼ããããªå¦çãç¶ããã¨ã«ãªãã¾ãã
ããã§Transitionã³ã³ãã¼ãã³ãã®åºçªã§ãã
ãã®ã³ã³ãã¼ãã³ãã使ãã¨ã大æµã®ãã¼ã¸é·ç§»ã¯ï¼è¡(one-liner)ã§æ¸ãããã«ãªãã¾ãã
ããã§ãªããã¼ã¸ããé·ç§»ç¨ã®å¦çã®è¨è¿°ãå°ãªããªããã¨ã§å¤§å¹
ã«ãã£ãããã¾ãã
â»æ³¨æ
ãã®ã³ã³ãã¼ãã³ãã¯ãå
é¨ã§ã»ãã·ã§ã³ã使ç¨ãã¦é·ç§»æã®ãã¼ã¿ãä¿æãã¦ãã¾ãã
ã»ãã·ã§ã³ãå©ç¨ä¸å¯è½ãªå ´åãã»ã¨ãã©ã®æ©è½ã使ããªããªããã¨ã«æ°ãã¤ãã¦ãã ããã
ä¾ç¤º
ç°¡åãªã³ã³ããã¼ã©ã®å®è£
ãè¦ã¦ã¿ã¾ãããã
ããã§ã¯ãUserã¢ãã«ãEnqueteã¢ãã«ã使ãã¾ãã
ï¼ãæ¶ç©ºã®CAPTCHAï¼ç»åèªè¨¼ï¼é¢æ°ãç¨ãã¦ãã¾ããå®è£
ãããã¨ããªãã®ã§é©å½ã§ãã
<?php class UsersController{ var $components = array('Transition'); // åºæ¬æ å ±å ¥åç»é¢ function register(){ $this->Transition->checkData('register_enquete'); } // ã¢ã³ã±ã¼ã function register_enquete(){ $this->Transition->automate( 'register_confirm', // 次ã®ã¢ã¯ã·ã§ã³ 'Enquete', // ã´ã¡ãªãã¼ã·ã§ã³ãè¡ãã¢ãã«å 'register_confirm' // åã®ã¢ã¯ã·ã§ã³ ); } // 確èªç»é¢ function register_confirm(){ $this->Transition->automate( 'register_save', //次 null, // ã«ã¬ã³ãã¢ãã«ã§ 'register_enquete', // å 'validateCaptcha' // æ¶ç©ºã®ç»åèªè¨¼ãæ¤è¨¼ãé¢æ° ); $this->set('data',$this->Transition->allData()); $this->set('captcha',createCaptcha()); // æ¶ç©ºã®ç»åèªè¨¼ãä½æãé¢æ° } // ä¿å function register_save(){ $this->Transition->checkPrev(array( 'register', // ãã® 'register_enquete', // ããã« 'register_confirm' // è¤æ°æå®ã§ãã¾ãã )); if($this->User->saveAll($this->Transition->mergedData()){ $this->Transition->clearData(); $this->Session->setFlash(__('Register complete !!',true)); $this->redirect(aa('action','index')); }else{ $this->Session->setFlash(__('Register failed ...',true)); $this->redirect(aa('action','register')); } } } ?>
ãã¦ãç»é¢ã®åæ©è½ã«ã¤ãã¦ã¯å¤§ä½ç解ã§ããã¨æãã¾ãã
- åºæ¬æ å ±å ¥åç»é¢ã§Userã¢ãã«ã«ä¿åããããã¼ã¿ãå ¥åããã
- ã¢ã³ã±ã¼ãç»é¢ã§Enqueteã¢ãã«ã«ä¿åããããã¼ã¿ãå ¥åããã
- 確èªç»é¢ã§ã¯ãå ¥åããããã¼ã¿ã®è¡¨ç¤ºã¨ãç»åèªè¨¼ãè¡ãã
- ä¿åããã¢ã¯ã·ã§ã³ã§ã¯saveAll()ãã¦ãã¾ãã
ã§ã¯ãåã¢ã¯ã·ã§ã³ã«æ²¿ã£ã¦è§£èª¬ãã¦ããããã¨æãã¾ãã
register()
checkData()ã¨ããã¡ã½ããã使ã£ã¦ãã¾ãã
ããã¯ãèªèº«ã®ãã©ã¼ã å
¥åã«ããªãã¼ã·ã§ã³ãã§ãã¯ãããéã£ãã次ã®ã¢ã¯ã·ã§ã³(第ä¸å¼æ°)ã«èªåãªãã¤ã¬ã¯ããã¾ãã
register_enquete()
automate()ã¨ããã¡ã½ããã使ã£ã¦ãã¾ãã
ããã¯ããã®ã³ã³ãã¼ãã³ãã®æ©è½ãä¸æ¬ã§è¡ã£ã¦ããããä¸æ ¸ã¡ã½ããã¨ãªãã¾ãã
åã®ã¢ã¯ã·ã§ã³ï¼ç¬¬ä¸å¼æ°ï¼ã®ãã¼ã¿ãã»ãã·ã§ã³ã«ããããã§ãã¯ãããªãã£ãããã®ã¢ã¯ã·ã§ã³ã«ãªãã¤ã¬ã¯ããã¾ãã
æ´ã«ãcheckData()ã¨åæ§ã®å¦çãè¡ãã¾ããããã¯ç¬¬ä¸å¼æ°ã§æ¬¡ã®ã¢ã¯ã·ã§ã³ã第äºå¼æ°ã§ã¢ãã«åãæå®ãã¾ãã
register_confirm()
å¼ãç¶ãautomate()ã使ã£ã¦ãã¾ãã
register_enquete()ã¨éãã®ã¯ã第ä¸å¼æ°ã®ã¢ãã«åã«nullãæå®ããã第åå¼æ°ãæå®ããã¦ãããã¨ã§ãã
第åå¼æ°ã«ã¯ç¬èªã®ããªãã¼ã·ã§ã³ã¡ã½ãããæå®ã§ãã¾ãã
æ¸å¼ã¯call_user_func()ãªã©ã«æ¸¡ãããcallbackå½¢å¼ã§ãã
渡ãããã¡ã½ããã¯ã(&$model,$data)ã¨ãã£ãå¼æ°ã§å¼ã³åºããã¾ãã
第äºå¼æ°ãnullï¼ããã©ã«ãï¼ã«ããã¨ãã³ã³ããã¼ã©ã®ã«ã¬ã³ãã®ã¢ãã«ï¼Controller::modelClassï¼ãèªåçã«åç
§ããã¾ãã
ãã®ã¢ãã«ã®æå®ã¯è¤æ°æå®ãå¯è½ã§ãæå®ãããã¢ãã«å
¨ã¦ã«å¯¾ãã¦ããªãã¼ããå®è¡ããã¾ãã
ä»åã¯ç¬èªã®ããªãã¼ã·ã§ã³ã¡ã½ããã使ãã®ã§ãåä¸ã®ã«ã¬ã³ãã¢ãã«ãã¯ãã¼ã¦ã«è¦ç«ã¦ã¦å®è¡ããã¦ãã¾ãã
æ´ã«ãallData()ã¨ããã¡ã½ããã使ã£ã¦ãã¾ãã
ããã¯ãã»ãã·ã§ã³ã«ä¿æããããã¼ã¿ãå
¨ã¦å¼ãåºãã¾ãã
ãã©ã¼ãããã¯ä»¥ä¸ã®ããã«ã¢ã¯ã·ã§ã³åããã¼ã«æã¡ã¾ãã
<?php array( 'register' => array( // ... register()ã§ä¿æããããã¼ã¿ ), // ...ä»ã®ã¢ã¯ã·ã§ã³ã®ãã¼ã¿ ) ?>
register_save()
checkPrev()ã¨ããã¡ã½ããã使ã£ã¦ãã¾ãã
ããã¯ãautomate()ã®ç¬¬ä¸å¼æ°ã®æ©è½ã«ããããåã®ã¢ã¯ã·ã§ã³ã®ãã¼ã¿ãã»ãã·ã§ã³ã«ããããã§ãã¯ãããªãã£ãããã®ã¢ã¯ã·ã§ã³ã«ãªãã¤ã¬ã¯ããã¾ãã
ã¢ã¯ã·ã§ã³åã¯ãæååã¨é
åã©ã¡ãã§ãæå®ã§ãã¾ãã
é
åãæå®ãããã¨ãæå®ãããå
¨ã¦ã®ã¢ã¯ã·ã§ã³ã®ã»ãã·ã§ã³ãã¼ã¿ããã§ãã¯ãã¾ãã
æ´ã«ãmergedData()ã¨ããã¡ã½ããã使ã£ã¦ãã¾ãã
ããã¯ãã»ãã·ã§ã³ã«ãããã¼ã¿å
¨ã¦ãå帰çã«ãã¼ã¸(array_merge_recursive())ãã¦è¿ãã¾ãã
ããã¯ä¿åæã«ãã確èªç»é¢ç¨ã«ã大å¤ä¾¿å©ã§ãã
æå¾ã«ãclearData()ã¨ããã¡ã½ããã使ã£ã¦ãã¾ãã
ããã¯ããã®ã³ã³ãã¼ãã³ãã§ä¿æãã¦ããã»ãã·ã§ã³ãã¼ã¿ãå
¨ã¦ç ´æ£ãã¾ãã
é·ç§»ç»é¢ãè¤æ°ããå ´åã¯ãdelData()ã¨ããã¡ã½ããã使ã£ã¦åå¥ã®ã¢ã¯ã·ã§ã³ã®ã»ãã·ã§ã³ãã¼ã¿ãç ´æ£ããå¿
è¦ãããã¾ãã
è£è¶³
ãã¦ã大ä½ã®ã¤ã¡ã¼ã¸ã沸ãããã¨æãã¾ãã
ãæ°ã¥ãã®ããã«ãå
¨ã¦ã®ã¢ã¯ã·ã§ã³ï¼ä¿åæãé¤ãï¼ã®ãã¥ã¼ã§ã®ããã©ã¼ã ã®æå®å
ã¯èªåèªèº«ã§ãã
<?php echo $form->create('User',array('action' => $this->action)) ?>
ã¨ãã£ãæå®ããã¦ãã ãããï¼1.3以éã使ã£ã¦ãããªãã$this->actionã¨ãã£ãæå®ã¯ä¸è¦ã«ãªãã¾ããï¼
ãã®ã³ã³ãã¼ãã³ãã®æ©è½é·ç§»å³ãè¼ãã¦ããã¾ãã
åã»çµµãæ±ãã®ã¯ä»æ§ã§ãã諦ãã¦ãã ããã
Q.ãªããªãã§ææ¸ãã«ããã A.ã¡ã¾ã¡ã¾PCã§å³æ¸ãã®ããã©ãããï¼å°ç¨ã®CASEãã¼ã«æã£ã¦ãªãã»ç¥ããªã
ãã®ä»æ©è½
- checkData()ãautomate()ã®ç¬¬ä¸å¼æ°ã¯Controller::redirect()ã«ç´æ¥æ¸¡ããã¾ãã
Transitionã³ã³ãã¼ãã³ãã¯è©³ç´°ãªè¨å®ãã§ãã¾ãã
以ä¸ã«ããããã£ã®è©³ç´°ã示ãã¦ããã¾ãã
ããã¯ã³ã¢ã³ã³ãã¼ãã³ãã¨åæ§ã«ã
<?php var $components = array('Transition' => array(/* ããããã£ã®è¨å® */)); ?>
ã¨ãã£ãæå®ãã§ãã¾ãã
<?php var $automation = false; ?>
ã¢ã¯ã·ã§ã³åãé
åã®ãã¼ã«ããã®ä»ããããã£ã®è¨å®ã®é
åãå¤ã«ã¨ãé
åãæå®ããã¨ãã³ã³ããã¼ã©ã®BeforeFilter()å¾ã«ãèªåçã«automate() ããã¾ãã
è¨ç½®ä¾ã¯ã³ã¼ãã«è¨è¼ãã¦ããã¾ããã¾ãã¯ãå¾ã«ä½ã詳細ãªãã¡ã¬ã³ã¹ãèªãã§ãã ããã
<?php var $messages = array(); ?>
åã®ã¢ã¯ã·ã§ã³ã®ãã¼ã¿ãåå¾ã§ããªãã£ãå ´åã¨ããªãã¼ã·ã§ã³å¤±ææãsetFlash()ã«æ¸¡ãããã¡ãã»ã¼ã¸ãæå®ãã¾ãããããã'prev'ã'invalid'ããã¼ã«ãªãã¾ããããããã®å¤ã«falseã渡ãã¨setFlash()ããã¾ããã
<?php var $autoComplete = true; ?>
ãã®ããããã£ãtrueã®å ´åãä¸åº¦ãã®ã¢ã¯ã·ã§ã³ã®ãã¼ã¿ãã»ãã·ã§ã³ã«ä¿æãããã¨ã次ã«åãã¢ã¯ã·ã§ã³ã«ã¢ã¯ã»ã¹ããã¨ããã»ãã·ã§ã³ãã¼ã¿ããèªåã§Controller::dataã«ãã¼ã¿ããªã¹ãã¢ãã¦ããã¾ãã
<?php var $autoRedirect = true; ?>
ãã®ããããã£ãfalseã«ããã¨ãèªåã§ãªãã¤ã¬ã¯ããè¡ãã¾ããã
<?php var $models = null; ?>
ããã©ã«ãã®ããªãã¼ã·ã§ã³ç¨ã¢ãã«ãæå®ã§ãã¾ãã
<?php var $validationMethod = null; ?>
ããã©ã«ãã®ããªãã¼ã·ã§ã³ã¡ã½ãããæå®ã§ãã¾ãã
詳細ãªAPIãªãã¡ã¬ã³ã¹
ã½ã¼ã¹ãè¦ã¦ãã ãããã¨è¨ãããã¨ããã§ãããå¾ã«æ¥æ¬èªã§åç §ã§ããããã«ã¾ã¨ãã¾ãã
æå¾ã«
ã¾ã ã¾ã ç´°ããã¨ããã§ä¸è¦ªåãªã¨ãããããã¾ãã
ä½ãå°ã£ãç¹ããã£ããã質åããã£ãããã·ãã·èãã¦ãã ããã
ä»å¾ã®éçºã«ã»ã¼å³åæ ããã¦ããã¾ãã:D
追è¨ï¼
é
·ãtypoãå¤ãã£ãã®ã§ï¼ç¹ã«ãµã³ãã«ã³ã¼ãï¼ãè¥å¹²ä¿®æ£ãã¾ãã:D