laravelÔõôÉèÖÃÒ³Ãæ
laravelÊÇÒ»ÖÖÊ¢ÐеÄphp¿ò¼Ü£¬ÆäÇ¿Ê¢µÄ¹¦Ð§ºÍÒ×ÓÚʹÓõĽçÃæÉè¼ÆʹÆä³ÉΪweb¿ª·¢Ö°Ô±µÄÊ×Ñ¡¹¤¾ßÖ®Ò»¡£ÔÚlaravelÖУ¬Ò³ÃæÉèÖÃÊÇÒ»¸öºÜÊÇÖ÷ÒªµÄ·½Ã棬ÓÉÓÚËü¿ÉÒÔ×ÊÖúÄú½«Ò³ÃæºÍÆäÏà¹ØµÄÊÓͼºÍÊý¾ÝÁªÏµÆðÀ´¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÏÈÈÝlaravelÖÐÔõÑùÉèÖÃÒ³ÃæµÄÒªÁì¡£
½¨ÉèÒ»¸öÊÓͼ
ÔÚLaravelÖУ¬ÊÓͼÊÇÈÏÕæäÖȾҳÃæµÄÄ£°å¡£Òª½¨ÉèÊÓͼ£¬¿ÉÒÔʹÓÃÒÔÏ°취£º
ÔÚresources/viewsĿ¼Ï½¨ÉèÒ»¸öеÄÎļþ¼Ð£¬ÓÃÓÚ´æ·ÅÄúµÄÊÓͼÎļþ¡£
ÔÚ¸ÃÎļþ¼ÐÖн¨ÉèÒ»¸öÃûΪhome.blade.phpµÄÎļþ£¬ÓÃÓÚ´æ·ÅÖ÷Ò³µÄHTML´úÂë¡£
ÔÚÎļþÖбàдHTML´úÂ룬Äú¿ÉÒÔʹÓÃLaravelµÄBladeÄ£°åÓï·¨À´°üÀ¨¶¯Ì¬ÄÚÈÝ¡£
ÀýÈ磬ÔÚhome.blade.phpÖаüÀ¨Ò»¸öÎÊÌâºÍ¶¯Ì¬ÄÚÈݵļòÆÓʾÀý£º
<!doctype html> <html> <head> <title>ÎÒµÄÍøÕ¾</title> </head> <body> <h1>½Ó´ýÀ´µ½ÎÒµÄÍøÕ¾</h1> <p>{{ $message }}</p> </body> </html>
µÇ¼ºó¸´ÖÆ
ÔÚ´ËʾÀýÖУ¬Ê¹ÓÃBladeÄ£°åÓï·¨{{ $message }}À´ÒýÓö¯Ì¬±äÁ¿$message£¬¸Ã±äÁ¿½«ÔÚ¿ØÖÆÆ÷ÖÐÉèÖò¢×ª´ï¸øÊÓͼ¡£
½¨ÉèÒ»¸ö¿ØÖÆÆ÷
ÔÚLaravelÖУ¬¿ØÖÆÆ÷ÊÇ´¦ÀíWebÇëÇó²¢·µ»ØÏìÓ¦µÄÀà¡£Òª½¨Éè¿ØÖÆÆ÷£¬¿ÉÒÔʹÓÃÒÔÏ°취£º
ÔÚapp/Http/ControllersĿ¼Ï½¨ÉèÒ»¸öÃûΪHomeController.phpµÄ¿ØÖÆÆ÷Îļþ¡£
±àд¿ØÖÆÆ÷´úÂ룬¸Ã´úÂ뽫¼ÓÔØÊÓͼ²¢·µ»ØHTMLÏìÓ¦¡£
ÀýÈ磬ÔÚHomeController.phpÖУ¬Äú¿ÉÒÔʹÓÃÒÔÏ´úÂëÀ´¼ÓÔØhome.blade.phpÊÓͼ²¢ÏòÆäת´ï¶¯Ì¬Êý¾Ý£º
<?php namespace AppHttpControllers; use IlluminateHttpRequest; class HomeController extends Controller { public function index() { $message = "½Ó´ýÀ´µ½ÎÒµÄÍøÕ¾"; return view('home', ['message' => $message]); } }
µÇ¼ºó¸´ÖÆ
ÔÚ´ËʾÀýÖУ¬index()ÒªÁì¼ÓÔØÃûΪhomeµÄÊÓͼ£¬²¢ÏòÆäת´ï±äÁ¿$message¡£
½¨ÉèÒ»¸ö·ÓÉ
ÔÚLaravelÖУ¬Â·ÓÉÊǽ«WebÇëÇóÓ³Éäµ½ÏìÓ¦µÄ¿ØÖÆÆ÷ÒªÁìµÄ»úÖÆ¡£Òª½¨Éè·ÓÉ£¬¿ÉÒÔʹÓÃÒÔÏ°취£º
·¿ªroutes/web.phpÎļþ¡£
ÔÚÎļþÖнç˵ÃûΪhomeµÄ·Óɲ¢½«ÆäÖ¸ÏòHomeController@indexÒªÁì¡£
ÀýÈ磬ÔÚweb.phpÖУ¬Äú¿ÉÒÔʹÓÃÒÔÏ´úÂëÀ´½ç˵home·ÓÉ£º
Route::get('/', 'HomeController@index')->name('home');
µÇ¼ºó¸´ÖÆ
ÔÚ´ËʾÀýÖУ¬get()ÒªÁì½ç˵ÃûΪhomeµÄ·ÓÉ£¬²¢½«ÆäÓ³Éäµ½HomeControllerÀàµÄindex()ÒªÁì¡£
´¦ÀíÒ³ÃæÊý¾Ý
ÔÚLaravelÖУ¬¿ÉÒÔʹÓÃÐí¶àÒªÁìÀ´´ÓÊý¾Ý¿â»òÆäËûÊý¾ÝÔ´»ñÈ¡Ò³ÃæÊý¾Ý¡£×î³£¼ûµÄÒªÁìÊÇʹÓÃEloquent ORM£¬ÕâÊÇLaravelµÄÄÚÖÃORM¡£ÒÔÏÂÊÇ´ÓÊý¾Ý¿âÖлñÈ¡Êý¾ÝµÄ¼òÆÓʾÀý£º
ÔÚHomeController.phpÖÐÌí¼ÓÒ»¸öеÄÒªÁ죬ÓÃÓÚ´ÓÊý¾Ý¿âÖлñÈ¡Êý¾Ý£º
public function getData() { $data = DB::table('users')->get(); return view('data', ['data' => $data]); }
µÇ¼ºó¸´ÖÆ
ÔÚ´ËʾÀýÖУ¬getData()ÒªÁìʹÓÃLaravelµÄQueryBuilder´ÓÊý¾Ý¿âÖмìË÷ËùÓÐÓû§µÄÊý¾Ý£¬²¢½«Êý¾Ýת´ï¸ødataÊÓͼ¡£
ÏÔʾҳÃæÊý¾Ý
Ò»µ©ÄúÔÚ¿ØÖÆÆ÷ÖÐÍøÂçÁËÊý¾Ý²¢½«Æäת´ï¸øÊÓͼ£¬¾Í¿ÉÒÔʹÓÃBladeÄ£°åÓï·¨½«ÆäÏÔʾÔÚÒ³ÃæÉÏ¡£ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄʾÀý£¬ÓÃÓÚÔÚÊÓͼÖÐÏÔʾ´ÓÊý¾Ý¿âÖлñÈ¡µÄÊý¾Ý£º
<!doctype html> <html> <head> <title>ÎÒµÄÊý¾Ý</title> </head> <body> <h1>ÎÒµÄÊý¾Ý</h1> <table> <thead> <tr> <th>ID</th> <th>Ãû×Ö</th> <th>µç×ÓÓʼþ</th> </tr> </thead> <tbody> @foreach ($data as $item) <tr> <td>{{ $item->id }}</td> <td>{{ $item->name }}</td> <td>{{ $item->email }}</td> </tr> @endforeach </tbody> </table> </body> </html>
µÇ¼ºó¸´ÖÆ
ÔÚ´ËʾÀýÖУ¬Ê¹ÓÃBladeÄ£°åÓï·¨@foreachÑ»·µü´úÊý¾Ý£¬²¢Ê¹ÓÃ{{ $item->name }}Óï·¨ÒýÓù¤¾ßÊôÐÔ¡£
×ܽá
Ò³ÃæÉèÖÃÊÇLaravelÖÐ×îÖ÷ÒªµÄ·½ÃæÖ®Ò»¡£½¨ÉèÊÓͼ¡¢¿ØÖÆÆ÷ºÍ·ÓÉ£¬ÒÔ¼°´¦ÀíºÍÏÔʾÊý¾Ý£¬¶¼ÊÇÉèÖÃÒ³ÃæµÄÖ÷Òª°ì·¨¡£Ê¹ÓÃÕâЩ¼òÆӵİ취£¬Äú¿ÉÒÔ¿ìËÙµØÉèÖÃLaravelÓ¦ÓóÌÐòµÄÒ³Ã棬²¢ÎªÓû§ÌṩÓÅÃÀµÄWebÌåÑé¡£
ÒÔÉϾÍÊÇlaravelÔõôÉèÖÃÒ³ÃæµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡