thinkphpÌøתҳ·â×°½Ì³Ì
thinkphpÊÇÒ»¸ö¿ªÔ´µÄphp¿ª·¢¿ò¼Ü£¬ËüÌṩÁËÇ¿Ê¢µÄmvcģʽ֧³Ö£¬ÉÁ¿ª·¢ÕßÄܹ»¿ìËÙ¿ª·¢ÎȽ¡µÄwebÓ¦Óá£ÔÚ¿ª·¢webÓ¦ÓÃÖУ¬¾³£ÐèÒª¾ÙÐÐÒ³ÃæÌøת£¬ÀýÈçÓû§µÇ¼ÀֳɺóÐèÒªÌøתµ½Óû§½çÃæ¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃthinkphp¾ÙÐÐÒ³ÃæÌøת£¬²¢·â×°Ò»¸öÌøתҳº¯Êý¡£
Ò»¡¢Ê¹ÓÃThinkPHP¾ÙÐÐÒ³ÃæÌøת
ThinkPHPÌṩÁËÁ½¸öÄÚÖú¯Êý¿ÉÒÔ¾ÙÐÐÒ³ÃæÌøת£º
redirect()º¯Êý
redirect()º¯ÊýÓÃÓÚÌøתµ½Ö¸¶¨µÄURLµØµã¡£ËüµÄÓï·¨ÈçÏ£º
redirect('url', '²ÎÊý', '״̬Âë')->send();
µÇ¼ºó¸´ÖÆ
ÆäÖУº
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
url£ºÒªÌøתµÄURLµØµã¡£
²ÎÊý£ºGET·½·¨µÄ²ÎÊý£¬¿ÉÒÔÊÇÊý×é»òÕß×Ö·û´®¡£
״̬Â룺HTTP״̬Â룬ÀýÈç302ÌåÏÖÖض¨Ïò£¬301ÌåÏÖÓÀÊÀÖض¨Ïò¡£
ÀýÈ磬ҪÌøתµ½http://www.example.com/user/indexÒ³Ã棬´úÂëÈçÏ£º
redirect('http://www.example.com/user/index')->send();
µÇ¼ºó¸´ÖÆ
success()ºÍerror()º¯Êý
success()ºÍerror()º¯ÊýÓÃÓÚÔÚÒ³ÃæÌøתʱÏÔʾһ¸öÌáÐÑÐÅÏ¢¡£ÀÖ³ÉÌáÐÑÐÅϢʹÓÃsuccess()º¯Êý£¬Ê§°ÜÌáÐÑÐÅϢʹÓÃerror()º¯Êý¡£ËüÃǵÄÓï·¨ÈçÏ£º
success('ÌáÐÑÐÅÏ¢', 'ÌøתURL', 'ÆÚ´ýʱ¼ä')->send(); error('ÌáÐÑÐÅÏ¢', 'ÌøתURL', 'ÆÚ´ýʱ¼ä')->send();
µÇ¼ºó¸´ÖÆ
ÆäÖУº
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ÌáÐÑÐÅÏ¢£ºÐèÒªÏÔʾµÄÐÅÏ¢£¬¿ÉÒÔÊÇ×Ö·û´®»òÊý×é¡£
ÌøתURL£ºÒªÌøתµÄURLµØµã£¬¿ÉÒÔÊ¡ÂÔ¡£
ÆÚ´ýʱ¼ä£ºÆÚ´ýʱ¼ä£¬µ¥Î»ÎªÃ룬ĬÒÔΪ1Ã룬¿ÉÒÔÊ¡ÂÔ¡£
ÀýÈ磬ҪÏÔʾһ¸öÀÖ³ÉÌáÐÑÐÅÏ¢²¢Ìøתµ½http://www.example.com/user/indexÒ³Ã棬´úÂëÈçÏ£º
success('µÇ¼ÀÖ³É', 'http://www.example.com/user/index')->send();
µÇ¼ºó¸´ÖÆ
¶þ¡¢·â×°Ìøתҳº¯Êý
ΪÁËÀû±ãÖظ´Ê¹Óã¬ÎÒÃÇ¿ÉÒÔ½«Ò³ÃæÌøת¾ÙÐзâ×°¡£ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄÌøתҳº¯Êý´úÂ룺
/** * Ìøתҳº¯Êý * * @param string $url ÒªÌøתµÄURLµØµã * @param string $message ÐÅÏ¢ÌáÐÑ * @param int $waitTime ÆÚ´ýʱ¼ä * @return void */ function jump($url, $message = '', $waitTime = 1) { if (empty($url)) { exit('¹ýʧ£ºÎ´Ö¸¶¨ÌøתURLµØµã£¡'); } if (!empty($message)) { $message = htmlspecialchars($message); } if ($waitTime == 0) { header("Location: {$url}"); exit; } $css = <<<EOF <style type="text/css"> .jump { text-align:center; padding-top:5%; font-family: 'Microsoft Yahei', Verdana, Arial; font-size:16px; } .jump h3 { font-size:24px; font-weight:bold; } </style> EOF; $html = <<<EOF <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>ÌøתÌáÐÑ</title> {$css} </head> <body> <div class="jump"> <h3>ÌøתÌáÐÑ</h3> <p>{$message}</p> <p>ÆÚ´ýʱ¼ä£º<span id="wait_time">{$waitTime}</span>Ãë</p> <p><a href="{$url}">Á¬Ã¦Ìøת</a></p> </div> <script type="text/javascript"> var wait_time = {$waitTime}; var interval = setInterval(function(){ if(wait_time > 0) { wait_time--; document.getElementById('wait_time').innerHTML = wait_time; } else { clearInterval(interval); window.location.href = '{$url}'; } }, 1000); </script> </body> </html> EOF; echo $html; }
µÇ¼ºó¸´ÖÆ
ʹÓÃÒÔÉϵķâ×°º¯Êý¿ÉÒÔÔÚ¿ØÖÆÆ÷ÖÐʵÏÖÒÔÏ´úÂ룺
public function login() { if($this->request->post()){ $data = $this->request->post(); // ÑéÖ¤ÂëÑéÖ¤ $user = UserModel::where('username', $data['username'])->find(); if(!$user || $user->password != $data['password']){ jump(url('login/index'), 'Óû§Ãû»òÃÜÂë¹ýʧ', 3); } else { jump(url('user/index'), 'µÇ¼ÀÖ³É', 3); } } return $this->fetch(); }
µÇ¼ºó¸´ÖÆ
ÒÔÉϾÍÊÇʹÓÃThinkPHP¾ÙÐÐÒ³ÃæÌøת²¢·â×°Ìøתҳº¯ÊýµÄ½Ì³Ì¡£Ê¹Ó÷â×°º¯Êý¿ÉÒÔÀû±ãµØÔÚ²î±ðµÄ¿ØÖÆÆ÷ÖÐÖظ´Ê¹Óá£
ÒÔÉϾÍÊÇthinkphpÌøתҳ·â×°½Ì³ÌµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡