ThinkPHP5ÖÐÔõôŲÓÃÒªÁì
Ò»¡¢¿ØÖÆÆ÷ÖеÄÒªÁìŲÓÃ
¿ØÖÆÆ÷ÊÇÓ¦ÓóÌÐòÖд¦ÀíHTTPÇëÇó²¢Ïòä¯ÀÀÆ÷·¢ËÍÏìÓ¦µÄµØ·½¡£ThinkPHP5ÖеĿØÖÆÆ÷Ö÷ҪλÓÚappĿ¼Ï£¬¿ØÖÆÆ÷µÄÃüÃû¹æÔòÊÇÒÔÍÕ·åʽÃüÃû·¨£¬ÀýÈ磺UserController.php¡£ÔÚ¿ØÖÆÆ÷ÖУ¬ÎÒÃÇ¿ÉÒÔ½ç˵¶à¸öÒªÁìÀ´ÏìÓ¦²î±ðµÄÇëÇó¡£ÍÕ·åʽÃüÃû·¨Ó¦¸ÃÊÊÓÃÓÚÒªÁìÃû³Æ£¬È磺indexAction()¡£
ҪŲÓÿØÖÆÆ÷ÖеÄÒªÁ죬ÐèҪͨ¹ýURLÀ´»á¼û¡£URLÖеÄÓòÃûºÍ¶Ë¿ÚºÅºóÃæ½ôËæ×ŵÄÊÇ¿ØÖÆÆ÷µÄÃû³Æ£¬ÀýÈ磺http://example.com/UserController¡£¶ø»á¼û¿ØÖÆÆ÷ÖеÄÒªÁ죬ÔòÐèÒªÔÚURLºóÃæÌí¼ÓÒªÁìÃû³Æ£¬ÀýÈ磺http://example.com/UserController/index¡£
ÎÒÃÇ¿ÉÒÔÔÚ¿ØÖÆÆ÷ÒªÁìÖÐʹÓÃÄÚÖú¯ÊýÀ´»ñÈ¡HTTPÇëÇó²ÎÊý¡¢»á¼ûÊý¾Ý¿â¡¢Êä³öÄ£°åµÈ¡£ÀýÈ磬ÏÂÃæµÄ´úÂëÑÝʾÁËÔõÑù»ñÈ¡HTTPÇëÇóÖеIJÎÊý£º
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
public function index() { $name = $this->request->param('name'); $age = $this->request->param('age'); echo "ÐÕÃû£º".$name."<br>ÄêË꣺".$age; }
µÇ¼ºó¸´ÖÆ
¶þ¡¢Ä£×ÓÖеÄÒªÁìŲÓÃ
Ä£×ÓÊÇÓ¦ÓóÌÐòÖÐÈÏÕæÖÎÀíÊý¾ÝµÄµØ·½¡£ThinkPHP5ÖеÄÄ£×ÓλÓÚappĿ¼ÏµÄmodelĿ¼ÖУ¬Ä£×ÓµÄÃüÃû¹æÔòÊÇÒÔÍÕ·åʽÃüÃû·¨£¬ÀýÈ磺UserModel.php¡£
ÔÚÄ£×ÓÖУ¬ÎÒÃÇ¿ÉÒÔ½ç˵¶à¸öÒªÁìÀ´ÖÎÀíÊý¾Ý¡£ÒªÁìÃû³ÆӦʹÓÃÍÕ·åʽÃüÃû·¨£¬ÀýÈ磺getUserInfo()¡£
ÔÚ¿ØÖÆÆ÷ÖÐʵÀý»¯Ä£×Ó£¬ÒÔ´ËÀ´Å²ÓÃÄ£×ÓÖеÄÒªÁì¡£ÀýÈ磬ÏÂÃæµÄ´úÂëÑÝʾÁËÔõÑùÔÚ¿ØÖÆÆ÷ÖÐŲÓÃÄ£×ÓÖеÄÒªÁ죺
public function index() { $userModel = new \app\model\UserModel(); $userInfo = $userModel->getUserInfo(1); echo "ÐÕÃû£º".$userInfo['name']."<br>ÄêË꣺".$userInfo['age']; }
µÇ¼ºó¸´ÖÆ
Èý¡¢ÊÓͼÖеÄÒªÁìŲÓÃ
ÊÓͼÊÇÓ¦ÓóÌÐòÖÐÈÏÕæչʾÊý¾ÝµÄµØ·½¡£ThinkPHP5ÖеÄÊÓͼλÓÚappĿ¼ÏµÄviewĿ¼ÖУ¬ÊÓͼµÄÃüÃû¹æÔòÊÇÒÔÍÕ·åʽÃüÃû·¨£¬²¢ÇÒÒÔ.tplΪºó׺Ãû£¬ÀýÈ磺userList.tpl¡£
ÔÚÊÓͼÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃÄÚÖú¯Êý»ò×Ô½ç˵º¯ÊýÀ´Õ¹Ê¾Êý¾Ý¡£ÏÂÃæµÄ´úÂëʾÀýչʾÁËÔõÑùÔÚÊÓͼÖÐŲÓÃÄÚÖú¯ÊýechoÊä³öÊý¾Ý£º
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Óû§Áбí</title> </head> <body> <table> <tr> <th>±àºÅ</th> <th>ÐÕÃû</th> <th>ÄêËê</th> </tr> <?php foreach($userList as $user) { ?> <tr> <td><?php echo $user['id']; ?></td> <td><?php echo $user['name']; ?></td> <td><?php echo $user['age']; ?></td> </tr> <?php } ?> </table> </body> </html>
µÇ¼ºó¸´ÖÆ
ÒÔÉϾÍÊÇThinkPHP5ÖÐÔõôŲÓÃÒªÁìµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡