ÔõÑùÔÚThinkPHP6ÖоÙÐÐÄ£°åÒýÇæ²Ù×÷£¿
ÔÚthinkphp6ÖУ¬Ä£°åÒýÇæÊǺÜÊÇÖ÷ÒªµÄÒ»²¿·Ö£¬Ëü¿ÉÒÔ×ÊÖúÎÒÃÇÔ½·¢¸ßЧµØ¾ÙÐÐÊÓͼäÖȾºÍÊý¾Ýչʾ¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚthinkphp6ÖоÙÐÐÄ£°åÒýÇæ²Ù×÷¡£
Ò»¡¢Ä£°åÒýÇæµÄ»ù´¡ÖªÊ¶
Ä£°åÒýÇæµÄ½ç˵
Ä£°åÒýÇæÊÇÒ»ÖÖ½«Êý¾Ýת»»³ÉHTMLµÄ¹¤¾ß£¬ËüµÄÖ÷Òª×÷ÓÃÊÇÊèÉ¢ÊÓͼºÍÓªÒµÂß¼¡£Í¨³£ÇéÐÎÏ£¬ÎÒÃǻὫÊý¾ÝºÍÊÓͼÍÑÀë´¦Àí£¬È»ºóͨ¹ýÄ£°åÒýÇ潫Á½Õß¾ÙÐÐÍŽᣬ×îÖÕ·ºÆð¸øÓû§¡£
Ä£°åÒýÇæµÄ·ÖÀà
ÔÚThinkPHP6ÖУ¬Ä£°åÒýÇæÖ÷Òª·ÖΪÁ½ÖÖ£ºÒ»ÖÖÊÇ»ùÓÚPHPµÄÄ£°åÒýÇ棨ÀýÈçSmarty¡¢BladeµÈ£©£¬ÁíÒ»ÖÖÊÇ»ùÓÚÔÉúÓï·¨µÄÄ£°åÒýÇæ¡£
Ä£°åÒýÇæµÄÓŵã
Ä£°åÒýÇæ¿ÉÒÔ×ÊÖúÎÒÃÇʵÏÖÊÓͼºÍÓªÒµÂß¼µÄÊèÉ¢£¬Ìá¸ß´úÂëµÄ¿Éά»¤ÐԺͿɶÁÐÔ£¬²¢ÇÒ¿ÉÒÔ¿ìËÙµÄʵÏÖÒ³Ãæ½á¹¹ÑùʽµÄ¸ü¸Ä£¬Ìá¸ß¿ª·¢Ð§ÂÊ¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
¶þ¡¢ThinkPHP6ÖеÄÄ£°åÒýÇæ²Ù×÷
Ä£°åÎļþµÄ½¨Éè
ÔÚThinkPHP6ÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁî¿ìËÙ½¨ÉèÒ»¸öÄ£°åÎļþ£º
php think make:view Index/index
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬IndexÌåÏÖ¿ØÖÆÆ÷Ãû³Æ£¬indexÌåÏÖÒªÁìÃû³Æ¡£Ö´ÐиÃÏÂÁîºó£¬»áÔÚapplicationĿ¼ÏÂ×Ô¶¯ÌìÉúÒ»¸öIndexĿ¼£¬²¢ÔÚ¸ÃĿ¼Ï½¨ÉèÒ»¸öindex.htmlµÄÎļþ¡£
±àдģ°åÎļþ
ÔÚ½¨ÉèÄ£°åÎļþÖ®ºó£¬ÎÒÃǾͿÉÒÔƾ֤×Ô¼ºµÄÐèÇó±àдHTML¡¢CSS¡¢JavaScriptµÈ´úÂëÁË¡£ÔÚÄ£°åÎļþÖУ¬»¹¿ÉÒÔͨ¹ýÄ£°åÒýÇæµÄÓï·¨À´Ç¶ÈëÊý¾Ý¡£
ÀýÈ磺
<html> <head> <title>Óû§Áбí</title> </head> <body> <table> <thead> <tr> <th>±àºÅ</th> <th>Óû§Ãû</th> <th>ÓÊÏä</th> <th>×¢²áʱ¼ä</th> </tr> </thead> <tbody> <?php foreach($users as $user): ?> <tr> <td><?php echo $user['id']; ?></td> <td><?php echo $user['username']; ?></td> <td><?php echo $user['email']; ?></td> <td><?php echo $user['create_time']; ?></td> </tr> <?php endforeach; ?> </tbody> </table> </body> </html>
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇʹÓÃÁËPHPµÄforeachÑ»·Óï¾äÀ´±éÀúÓû§ÁбíÊý¾Ý£¬²¢½«Êý¾Ý·ºÆðµ½HTMLÒ³ÃæÖС£
Ä£°å±äÁ¿µÄ¸³ÖµºÍʹÓÃ
ÔÚThinkPHP6ÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃcontrollerÖеÄassignÒªÁìÀ´¸øÄ£°åÎļþÉèÖñäÁ¿¡£
ÀýÈ磺
public function index() { // »ñÈ¡Óû§Êý¾Ý $users = Db::name('user')->select(); // ÉèÖÃÄ£°å±äÁ¿ $this->assign('users', $users); // äÖȾģ°åÊä³ö return $this->view->fetch(); }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇÊ×ÏÈͨ¹ýDb::name(‘user’)->select()ÒªÁì»ñÈ¡ÁËÓû§Êý¾Ý£¬È»ºóͨ¹ý$this->assign()ÒªÁ콫Êý¾ÝÉèÖõ½Ä£°å±äÁ¿ÖС£×îºóͨ¹ýreturn $this->view->fetch()ÒªÁ콫ģ°åÎļþäÖȾΪHTMLÒ³Ã棬²¢Êä³öµ½ä¯ÀÀÆ÷¡£
ÔÚÄ£°åÎļþÖУ¬¿ÉÒÔͨ¹ý{{$±äÁ¿Ãû}}µÄÓï·¨À´»ñÈ¡Ö¸¶¨µÄ±äÁ¿Öµ¡£
ÀýÈ磺
@foreach($users as $user) <tr> <td>{{$user['id']}}</td> <td>{{$user['username']}}</td> <td>{{$user['email']}}</td> <td>{{$user['create_time']}}</td> </tr> @endforeach
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇʹÓÃÁË{{$}}Óï·¨À´»ñÈ¡Óû§Êý¾ÝÖеĶÔÓ¦Öµ£¬²¢½«Æäչʾµ½HTMLÒ³ÃæÖС£
Ä£°å½á¹¹µÄʵÏÖ
ÔÚÏÖʵ¿ª·¢ÖУ¬ÎÒÃÇͨ³£»á½«ËùÓеÄÒ³Ãæ½á¹¹ÖеĹ«¹²´úÂ루ÀýÈçÍ·²¿¡¢Î²²¿¡¢²à±ßÀ¸µÈ£©¾ÙÐÐÌáÈ¡£¬ÒÔ±ãÔÚÆäËûÒ³ÃæÖоÙÐÐÖظ´Ê¹Óá£
ÔÚThinkPHP6ÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýʹÓÃÄ£°å½á¹¹À´ÊµÏָù¦Ð§£¬Ïêϸ²Ù×÷ÈçÏ£º
1£©ÔÚapplicationĿ¼Ï½¨ÉèÒ»¸ölayoutĿ¼£¬²¢ÔÚ¸ÃĿ¼Ï½¨ÉèÒ»¸öbase.htmlÎļþ¡£
2£©ÔÚbase.htmlÎļþÖÐÉèÖÃÒ³Ãæ½á¹¹£¬ÈçÍ·²¿¡¢Î²²¿µÈ¹«¹²´úÂë¡£
ÀýÈ磺
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>{{$title}}</title> </head> <body> <header> <!-- Í·²¿´úÂë --> </header> <main> <!-- Ö÷Ìå´úÂë --> </main> <footer> <!-- µ×²¿´úÂë --> </footer> </body> </html>
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇÉèÖÃÁËHTMLÒ³ÃæµÄ»ù±¾½á¹¹£¬²¢Ê¹ÓÃ{{$}}Óï·¨À´»ñÈ¡±äÁ¿Öµ¡£
3£©ÔÚÆäËûµÄÄ£°åÎļþÖÐʹÓÃextendsºÍsectionÓï·¨À´¼ÌÐøºÍʹÓù«¹²½á¹¹Îļþ¡£
ÀýÈ磺
@extends('layout/base') @section('content') <table> <thead> <tr> <th>±àºÅ</th> <th>Óû§Ãû</th> <th>ÓÊÏä</th> <th>×¢²áʱ¼ä</th> </tr> </thead> <tbody> @foreach($users as $user) <tr> <td>{{$user['id']}}</td> <td>{{$user['username']}}</td> <td>{{$user['email']}}</td> <td>{{$user['create_time']}}</td> </tr> @endforeach </tbody> </table> @endsection
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇÊ×ÏÈʹÓÃ@extendsÓï·¨À´¼ÌÐø¹«¹²½á¹¹Îļþ£¬È»ºóʹÓÃ@sectionºÍ@endsectionÓï·¨À´ÊµÏÖÄ£°åÄÚÈݵÄÌæ»»ºÍÀ©Õ¹¡£
½áÓï
ͨ¹ý±¾ÎĵÄÏÈÈÝ£¬¶ÁÕßÓ¦¸ÃÒѾÏàʶÁËÔÚThinkPHP6ÖÐÔõÑù¾ÙÐÐÄ£°åÒýÇæ²Ù×÷£¬°üÀ¨Ä£°åÎļþµÄ½¨É衢ģ°å±äÁ¿µÄ¸³ÖµºÍʹÓá¢Ä£°å½á¹¹µÄʵÏֵȡ£Ä£°åÒýÇæÊÇWeb¿ª·¢µÄÖ÷ÒªÊÖÒÕ£¬ÕÆÎպøÃÊÖÒÕ¿ÉÒÔÌá¸ß¿ª·¢Ð§ÂʺʹúÂë¿Éά»¤ÐÔ¡£
ÒÔÉϾÍÊÇÔõÑùÔÚThinkPHP6ÖоÙÐÐÄ£°åÒýÇæ²Ù×÷£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡