ãæå³ã§æ¬ãå ±éåãã¨ãæ©è½ã§æ¬ãå ±éåã
ã¡ã¨facebookã§ãããã¿ããã£ãã®ã§ã軽ãå±éã
å
ãã¿ã§æ¸ãã¦ããå
容ã¨ãã¦ã¯å¤§ã¾ãã«
ã»ï¼ã¤ã®form(ãã°ã¤ã³ã¨æ°è¦ç»é²)ã§ããããvalidate(ä»åã¯ä¸æ¦ãå¿
é ãã§ãã¯(空ç½ãã§ãã¯)ã®ã¿)ãæ¸ããã
ã»åformãã¨ã«ç©ºç½ãã§ãã¯ãæ¸ãã¦ããã®ã¯ç¡é§ãªã®ã§ãã¾ãã¾ã¨ããã
ã£ã¦ããã話ãã
ããã¡ããã®çµè«ã¨ãã¦ã¯
ã»ã¾ããåã
ã®formæ¯ã«classãåãããPHPã®ä¸è¬çãã¬ã¼ã ã¯ã¼ã¯ãªãModelç¶æ¿ã¯ã©ã¹ããã¡ã®MagicWeaponãªãdata_clumpç¶æ¿ã¯ã©ã¹
ã»ã空ç½ãã§ãã¯ãã¯å
±éåãã¦ããããã ãã©å¤åï¼è¡ã§æ¸ãã¡ããããããã¾ãæ¨ã¿ã¯ãªãï½
ã£ã¦ã¨ããããªãã
ãã¿ç ãã¦ã
ãPHPã®ä¸è¬çã«MVCã¨å¼ã°ããã¨ããã®ãã¬ã¼ã ã¯ã¼ã¯ã«ãããModelããããæå³ããã®ããã«ã¤ãã¦ã®è°è«ã¯è²ã
ãããããã¨æãã®ã§ããã
ã¨ãããããä¸å¡ã®ãã¼ã¿ç¾¤ãã¨ããã®ãã¼ã¿ç¾¤ã«å¯¾ããå¦çãã¯ãModelã«æ¸ãã¦ãããæ¹ããªã«ãã¨ãã³ã³ããã¼ã©ã¼ã太ããªããã¨æãã®ã§ãã ãã
ãªã®ã§â¦ãã£ãæ¦å¿µçã³ã¼ãã§ããã
ã³ã³ããã¼ã©ã®ä¸ã§ã¯ãä¾ãã°ä¸è¿°formã®ã話ãã§ããã°ãä¾ãã°æ°è¦ç»é²ãªã
class æ°è¦ç»é²ã³ã³ããã¼ã© { // pub func æ°è¦ç»é²ç¢ºèª() { // $model = new æ°è¦ç»é²model(); $model->CGIããã®å ¥åãã¼ã¿ãã¤ã³ã¹ã¿ã³ã¹ãä¸ã«å ¥ãã(); // å¼æ°ã¯ç¡ãã§ããããcgi requestã¯ã©ã¹ã¨ãããããªããã渡ãã°ããã // 空ç½ãã§ãã¯ããã if (false === $model->validate()) { $model->validateã¨ã©ã¼ã®æ å ±ãviewã«å ¥ãã(); // å¼æ°ã§viewã¤ã³ã¹ã¿ã³ã¹æ¸¡ãããããæ»ãå¤ãããã·ã¥é åã«ãã¦viewã«è¨å®ãããããï¼ ã¨ã©ã¼ç¨viewã表示(); return ; } // XXX ããã¾ã§ãããåé¡ãªãã¯ããªã®ã§ $model->modelèªèº«ã®æ å ±ãã»ãã·ã§ã³ã«æ¸ãè¾¼ã(); 確èªç»é¢ç¨viewã表示(); } // pub func æ°è¦ç»é²å®äº() { // $model = new æ°è¦ç»é²model(); $r = $model->modelèªèº«ã®æ å ±ãã»ãã·ã§ã³ããèªã¿è¾¼ã(); if (false === $r) { ããåã©ã£ããå ¥ã£ã¦ãããã¢ããããããªã«ã¼ãéã£ã¦ãããã¢ãããï¼ãã¨ã©ã¼è¡¨ç¤º return ; } // DBã«æ¸ãè¾¼ã $r = $model->insert(); // å¼æ°ã«ãå¿ è¦ãªãDBãã³ãã«ã¨ã渡ã if (false === $r) { ããªããç»é²ãã¾ããããªãã£ãããããä¸åº¦è©¦ãã¦ã¿ã¦ããã¨ã©ã¼ return ; } // ã»ãã·ã§ã³æ å ±ã¯ãªã¢ãã¦çµäº $model->modelèªèº«ã®æ å ±ããããã»ãã·ã§ã³æ å ±ãæ¶ã(); 確èªç»é¢ç¨viewã表示(); }
ã£ã¦æãã«ãªã£ã¦ãæ ã«modelã¨ãã¦ã¯
CGIããã®å ¥åãã¼ã¿ãã¤ã³ã¹ã¿ã³ã¹ãä¸ã«å ¥ãã(); // å¼æ°ã¯ç¡ãã§ããããcgi requestã¯ã©ã¹ã¨ãããããªããã渡ãã°ããã validate(); validateã¨ã©ã¼ã®æ å ±ãviewã«å ¥ãã(); // å¼æ°ã§viewã¤ã³ã¹ã¿ã³ã¹æ¸¡ãããããæ»ãå¤ãããã·ã¥é åã«ãã¦viewã«è¨å®ãããããï¼ modelèªèº«ã®æ å ±ãã»ãã·ã§ã³ã«æ¸ãè¾¼ã(); modelèªèº«ã®æ å ±ãã»ãã·ã§ã³ããèªã¿è¾¼ã(); insert(); // å¼æ°ã«ãå¿ è¦ãªãDBãã³ãã«ã¨ã渡ã modelèªèº«ã®æ å ±ããããã»ãã·ã§ã³æ å ±ãæ¶ã();
ãå¿
è¦ãªãã¨ãã大ä½è¦ã¦åããã§ãã
ã¡ãªã¿ã«ç¢ºèªç»é¢ãªããªã
CGIããã®å ¥åãã¼ã¿ãã¤ã³ã¹ã¿ã³ã¹ãä¸ã«å ¥ãã(); // å¼æ°ã¯ç¡ãã§ããããcgi requestã¯ã©ã¹ã¨ãããããªããã渡ãã°ããã validate(); validateã¨ã©ã¼ã®æ å ±ãviewã«å ¥ãã(); // å¼æ°ã§viewã¤ã³ã¹ã¿ã³ã¹æ¸¡ãããããæ»ãå¤ãããã·ã¥é åã«ãã¦viewã«è¨å®ãããããï¼ insert(); // å¼æ°ã«ãå¿ è¦ãªãDBãã³ãã«ã¨ã渡ã
ã§è¶³ããã®ã§ã大å¤ã«å·¥æ°ãã¨ã³ããã¼ã«ãªãã¾ãï½
ãã¤ã³ãã¯ãcontrollerã¯ããããã£ã¦ãã£ã¦ãããã£ã±ãªå½ä»¤ãmodelã«æ¸¡ããã¨ã§ãmodelã¯ãããããã£ã¦åãåã£ã¦ãããããã
ãããªé¢¨ã«ããã£ã¦æ¬²ããåä½ããã¡ã½ããã«ãã¦ãã£ã¦æãã§controllerãæ¸ãã¦ããã¨ãå²åã¨ã³ã³ããã¼ã©ã¼ããã¤ã¨ããåºæ¥ãã¨æãã®ã§ãã ãã
çºæ³ã¨ãã¦ã¯å²ã¨ãã¼ã·ãã¯ã«
ã»ããï¼ã¤ã®ãã¼ã¿ã®å¡ãã¨ããã®å¡ã«å¯¾ããå¦çããã²ã¨ã¾ã¨ãã«
ã£ã¦æãã
ãä¸å¡ã®ãã¼ã¿ãã£ã¦ã®ã¯ããã¯ãæå³ã®ãããã®ããªã®ã§ãããã«ã¤ãã¦ã¯ã²ã¨ã¾ã¨ãã
ãã§ãä¾ãã°ãã®ãä¸å¡ã®ãã¼ã¿ã«å¯¾ããvalidateãã§ããã°ãããã¯ãvalidateã¨ããæå³ãæã¤å¦çããå
±éå(ã¡ã½ããå)ãã¦ããã¾ããããççºæ³ã
ãã®è¾ºãããã®ã§ãã¾ãã¯ãæå³ã§æ¬ãå ±éåãããã£ããã¨æèãã¦ããã¨ããã¨æãã®ã§ãããã¯å¿ é ã
ãã§ã
ãæå³çã«ã¯ç°ãªããã ãã©ãå¦ççã«ã¯åãã ããããã£ã¦æ©è½ãããã§ãã
ä»åã®ãã¿ã ã¨ã空ãã©ãããã ãããã以å¤ã«ãä¾ãã°ããåºãä¾ãã ã¨ãæ¶è²»ç¨ã®8%è¨ç®ãã¨ãææ°æã®8%è¨ç®ãã¯ã©ã¡ãããå
å¤ã®8%ãè¨ç®ãããç¨åº¦ã«ã¯ä¸ç·ã®æ©è½ã«ãªãã¾ã(端æ°å¦çã¨ããåè´ãã¦ããåæ)ã
ãã®å ´åã®å
±éåã§ããã
ã¾ããæå³ã¬ã¤ã¤ã¼ãã§ã¯å½ç¶ç°ãªãã®ã§ãæå³ã¬ã¤ã¤ã¼çã«ã¯ãå¥ã
ã®ã¡ã½ããã«ããã¹ããã§ãããã¯å¿
é ã
ããã§æ··ããã¨ãå¾ã§é¢åããªã®ã§ãå
¥ãå£ã¯ã¡ããã¨åãåããã¹ãã
ãã ãæ©è½ãã¨ããããä¸ç·ããªã®ã§ããã°ããæå³ã¬ã¤ã¤ã¼ã®ç°ãªãå
¥ãå£ããåä¸ã®ã¡ã½ããã"ã¨ãããã"å¼ã¶ãã¯ãååã«ããããªããã¨ã
ãã®å ´åãæ©è½ãç°ãªããããªå¤æ´ãå
¥ã£ãããã®æç¹ã§ä¿®æ£ããã°ãããã®ã§ãå
±éåãã¦ãããã¾ãå°ããªãã¦ã
ã§ãå
±éåãã¦å°ããªããã®ã§ããã°ãã¾ããåãæ©è½ã®ã³ã¼ããããã³ããããããã¯ã¾ãå
±éåããã»ãããè²ã
ã¨å¡©æ¢
ããããããããã¨ã
ã¨ããããã§ã
åºæ¬çã«ãæå³ã¬ã¤ã¤ã¼ã®å
±éåãã¯ãã¹ãã ããéã«ãæå³ã¬ã¤ã¤ã¼ãç°ãªããã®ã§ããã°ãåä¸å¦çã§ãã£ã¦ãä¸æ¦ãæä½éã¡ã½ãããªãã¯ã©ã¹ãªãã¯åãåããã¹ãã§ãã
ãã ãåãå¦çãã§ããã°ãæå³ã¬ã¤ã¤ã¼ã®å¥¥ã§ãåãæ©è½ã¬ã¤ã¤ã¼ãcallãããã®ã¯æ®éã«ãããªããããªãããªããã¨ã楽ã ãã
ã§ãå°ãå±æçãªã話ãã
ãç°ãªãformã®ãå¿
é ãã©ããããã§ãã¯ãããã¸ãã¯ã¯ã¾ã¨ããããã£ã¦ã®ã¯ãä¾ãã°ããã¡ãããªããããªé¢¨ã«ããããªããçãªä¸ä¾ã
ã¾ãã¯ããªãªã¸ãã«ãªModelã®åºåºã¯ã©ã¹ããä½ãã§ãã
ããããã®ãã¬ã¼ã ã¯ã¼ã¯ã§ã¾ã大æµãModelãã£ã¦ã¯ã©ã¹åã ã¨æãã®ã§ããããåæã«
class gallu_model extends model { // å¿ é ãã§ãã¯(ã¨ããããPOSTåæ) // ä¸ã¤ã§ã空ãããã°falseãå ¨é¨åã¾ã£ã¦ãã°trueãreturn public function validate_isempty_main(array $params){ // ããã®ãã©ã¡ã¿ã空ã ã£ããï¼ãé åãä¸æ¦åæå $this->empty_params_ = array(); // æå®ããããã©ã¡ã¿ããã§ãã¯ãã foreach($params as $p) { if (true === empty($_POST[$p])) { // å 容ã空ã£ã½ã ã£ããã空ã ã£ããï¼ãé åã«å ¥ãã¦ãã $this->empty_params_[] = $p; } } // ã空ã ã£ããï¼ãé åã空ã£ã½ã®ã¾ã¾ãªãtrue(å ¨é¨åã¾ã£ã¦ããï¼) if (true === empty($this->empty_params_)) { return true; } // else // 空ã£ã½ã®ãã¼ã¿ããã£ãã orz return false; } // ã空ã ã£ããï¼ãé åãåå¾ public function get_empty_params() { return $this->empty_params_; } //private: private $empty_params_; }
ãããªã®ãä½ã£ã¦ããã¦ã
å¾ã¯ãåå¥ã®modelã§â¦ä¾ãã°loginã®modelã§ããã°
class login_model extends gallu_model { // validate public function validate_isempty(){ // ã¨ãããããå¿ é é ç®ãã®ãªã¹ããä½ã $params = array('id', 'pass'); // validateæ¬ä½ãcall return $this->validate_isempty_main($params); } }
ã¦ã¼ã¶ç»é²ã§ããã°
class user_model extends gallu_model { // validate public function validate_isempty(){ // ã¨ãããããå¿ é é ç®ãã®ãªã¹ããä½ã $params = array('id', 'pass', 'name'); // validateæ¬ä½ãcall return $this->validate_isempty_main($params); } }
ã¨ãã£ã¦ããã¨ãããç¨åº¦
ã»æå³ã§å
±éåã§ãã¦
ã»ã¤ãã§ã«æ©è½ã®å
±éåãåºæ¥ã
ã®ããªãããªã©ã¨ã
ã¾ããã©ã®å¦çãcontrollerã«æ¸ãã¦ã©ã®å¦çãmodelã«æ¸ãã(viewã«å¦çãæ¸ã輩ã«ç¦ã¨åªããã)ãã£ã¦ã®ã¯è²ã
è°è«ãããã¨ããã ã¨æãã®ã§ããã
ããã¡ããã¯ãã¾ããããããPHPãã¬ã¼ã ã¯ã¼ã¯ã®MVCãã§ããã°ãä¸è¿°ã®ãããªæããå²åã¨ã好ã¿ããªããã¨ã