ʹÓÃlaravelÀ´´î½¨php
Ëæ×Å»¥ÁªÍøÊÖÒյĿìËÙÉú³¤£¬webÓ¦ÓóÌÐòµÄÐèÇóÒ²Ô½À´Ô½¸ß¡£¶ø×÷Ϊ»¥ÁªÍøÌìÏÂÖÐ×î³£Óõıà³ÌÓïÑÔ£¬phpµÄʹÓùæÄ£Ò²×ãÒÔϯ¾íwebÓ¦ÓóÌÐòµÄ¾ø´ó²¿·Ö¡£¶ølaravel¿ò¼ÜÔòÊÇphp¿ª·¢Õß½¨ÉèÔ½·¢¸ßЧµÄwebÓ¦ÓóÌÐòµÄ±Ø±¸¹¤¾ßÖ®Ò»¡£ÔÚ´ËÎÄÕÂÖУ¬ÎÒÃǽ«Ì½ÌÖÔõÑùʹÓÃlaravel¿ò¼ÜÀ´´î½¨¸ßÖÊÁ¿µÄwebÓ¦ÓóÌÐò¡£
Ò»¡¢Laravel¿ò¼Ü¼ò½é
LaravelÊÇÒ»¸ö¿ªÔ´µÄPHP Web¿ò¼Ü£¬ËüµÄ³õʼ°æ±¾Ðû²¼ÓÚ2011Äê¡£Laravel¿ò¼ÜÓÃÓÚ¿ª·¢WebÓ¦ÓóÌÐò£¬ÌØÊâÊÇMVCģʽ£¨Model-View-Controller£©¹¹¼ÜϵÄWebÓ¦ÓóÌÐò£¬ÒÔ¼°RESTful API¡£Laravel¿ò¼ÜÊÇÒ»¸öÃâ·ÑµÄ¿ò¼Ü£¬²¢»ùÓÚMITÔÊÐíÖ¤¿ªÔ´¡£
¶þ¡¢Laravel¿ò¼ÜµÄÌØÕ÷
1¡¢¾«Á·ÎÞа
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
Laravel¿ò¼Ü¾ßÓкܺõÄÎÞаÐԺ;«Á·ÐÔ£¬ËüÓÐÒ»¸öºÜÊǼòÆÓ¶øÇå½àµÄ¿ò¼Ü½á¹¹£¬²¢ÇÒÒ×ÓÚʹÓúÍѧϰ¡£¿ÉÒÔͨ¹ýComposerÀ´ÔöÌí»òɾ³ýLaravelµÄ¹¦Ð§Ä£¿é£¬ÈÃLaravel¿ò¼ÜÔ½·¢ÎÞа¡£
2¡¢Â·ÓÉϵͳ
Laravel¿ò¼ÜʹÓÃÒ»¸öÇ¿Ê¢µÄ·ÓÉϵͳ£¬¿ÉÒÔÇáËɵؽç˵ӦÓóÌÐòµÄ·ÓÉ¡£½ç˵·ÓÉÊǺÜÊÇÀû±ã£¬¿ÉÒÔʹÓÃRESTful APIÆøÑæÆøÑæµÄ·ÓÉ£¬Í¬Ê±Ò²Ö§³Ö¶àÖÐÑë¼þºÍÇëÇó²ÎÊý£¬¿ÉÒÔÖª×ã²î±ðµÄÐèÇó¡£
3¡¢Êý¾ÝǨáã
Laravel¿ò¼ÜÌṩÁËÒ»¸öºÜÊÇÀû±ãµÄÊý¾ÝǨá㹤¾ß£¬¿ÉÒÔÔÚ¿ª·¢Àú³ÌÖÐÇáËɵش¦ÀíÊý¾Ý¿â±íµÄ½¨Éè¡¢Ð޸ĺÍɾ³ýµÈ²Ù×÷¡£²¢ÇÒÕâЩ²Ù×÷ÊÇ¿É°æ±¾¿ØÖƵģ¬Ê¹µÃÍŶӿª·¢Ô½·¢Ë³³©¡£
4¡¢ORM
Laravel¿ò¼ÜʹÓÃEloquentÀ´ÊµÏÖ¹¤¾ß¹ØϵӳÉ䣨ORM£©£¬¿ÉÒÔÉÁ¿ª·¢ÕßÇáËɵØÓëÊý¾Ý¿â¾ÙÐн»»¥£¬¶ø²»±Øµ£ÐÄSQLÓï¾äµÄÖØ´óºÍ·±Ëö¡£³ýÁËEloquent£¬Laravel¿ò¼Ü»¹Ö§³ÖÀàËÆDoctrineºÍPropelµÄORM½â¾ö¼Æ»®¡£
5¡¢BladeÄ£°åÒýÇæ
Laravel¿ò¼ÜʹÓÃBladeÄ£°åÒýÇ棬¿ÉÒÔÇáËɵؽ«Êý¾Ý×¢Èëµ½ÊÓͼÖУ¬Í¬Ê±Ò²Ö§³Ö½á¹¹¡¢Ìõ¼þºÍÑ»·µÈ³£¼ûµÄÄ£°åÓï·¨¡£BladeÄ£°åÒýÇæºÜÊÇÇ¿Ê¢£¬¿ÉÊÇÒ²ºÜÊÇÒ×ÓÚʹÓá£
Èý¡¢Ê¹ÓÃLaravel¿ò¼ÜÀ´´î½¨WebÓ¦ÓóÌÐò
ÏÂÃ棬ÎÒÃǽ«´ÓÒÔϼ¸¸ö·½ÃæÀ´ËµÃ÷ÔõÑùʹÓÃLaravel¿ò¼ÜÀ´´î½¨WebÓ¦ÓóÌÐò¡£
1¡¢×°ÖÃLaravel¿ò¼Ü
×°ÖÃLaravel¿ò¼ÜºÜÊǼòÆÓ£¬¿ÉÒÔͨ¹ýʹÓÃComposer£¬Ö±½Ó´ÓÏÂÁîÐÐ×°ÖÃLaravel¡£¿ÉÒԲο¼ÒÔÏÂÏÂÁ
composer create-project –prefer-dist laravel/laravel project_name
Õ⽫´ÓPackagistÉÏ×Ô¶¯ÏÂÔز¢×°ÖÃ×îа汾µÄLaravel¿ò¼Ü¡£
2¡¢½¨Éè»ù±¾Â·ÓÉ
ÔÚLaravel¿ò¼ÜÖУ¬Â·Óɱ»½ç˵ÔÚroutes/web.phpÎļþÖС£¿ÉÒÔÔÚ¸ÃÎļþÖмÓÈëÒÔÏ´úÂ룺
Route::get(‘/’, function () { return view(‘welcome’); });
Õ⽫»á½ç˵һ¸öGETÇëÇóµÄ»ù±¾Â·ÓÉ£¬Ö¸Ïò¸ù·¾¶´¦£¬²¢ÇÒ·µ»ØwelcomeÊÓͼ¡£ÊÓͼ¿ÉÒÔÔÚresources/viewsĿ¼ÏÂÃ棬½¨ÉèÒ»¸öwelcome.blade.phpÎĵµ¼´¿É¡£
3¡¢½¨Éè¿ØÖÆÆ÷
ÔÚLaravel¿ò¼ÜÖУ¬¿ØÖÆÆ÷ÈÏÕæ´¦ÀíËùÓеÄÓªÒµÂß¼¡£¿ÉÒÔʹÓÃArtisan¹¤¾ßÀ´½¨ÉèÒ»¸ö¿ØÖÆÆ÷£¬¿ÉÒԲο¼ÒÔÏÂÏÂÁ
php artisan make:controller UserController
Õ⽫½¨ÉèÒ»¸öÃûΪUserControllerµÄ¿ØÖÆÆ÷£¬ÔÚapp/Http/ControllersĿ¼ÏÂÃæ¡£
ÏÖÔÚ£¬¿ÉÒÔÔÚUserControllerÖнç˵һЩҪÁìÀ´´¦Àí·ÓÉ£¬ºÃ±È£º
public function index() { return view(‘users.show’, [‘name’ => ‘Taylor’]); }
ÕâÀïÎÒÃǽç˵ÁËÒ»¸öÃûΪindexµÄÒªÁ죬·µ»ØÁËÒ»¸öÃûΪusers.showµÄÊÓͼ£¬²¢ÇÒ´«ÈëÁËÒ»¸ö±äÁ¿name¡£
4¡¢½ç˵ÊÓͼ
ÔÚLaravel¿ò¼ÜÖУ¬ÊÓͼÎļþ¿ÉÒÔ´æ·ÅÔÚresources/viewsĿ¼ÏÂÃæ¡£ÆäÖУ¬ÊÓͼÎļþµÄÀ©Õ¹ÃûΪ.blade.php¡£
ÀýÈ磬ÔÚresources/views/users/show.blade.phpÎļþÖмÓÈëÒÔÏ´úÂ룺
@yield(‘title’) @yield(‘content’)
ÕâÀïÎÒÃǽç˵ÁËÒ»¸ö»ù±¾µÄHTMLÄ£°å£¬²¢ÇÒʹÓÃÁËBladeÄ£°åÒýÇæµÄ@yieldÖ¸Á½ç˵ÁËtitleºÍcontentÁ½¸öÇøÓò¡£ÕâÑù£¬ÔÚÏêϸµÄÊÓͼÎļþÖпÉÒÔ¼ÌÐø¸Ã»ù±¾Ä£°å£¬½ç˵ÏêϸµÄÄÚÈÝ¡£
ÀýÈ磬ÔÚresources/views/users/index.blade.phpÎļþÖпÉÒÔ¼ÓÈëÒÔÏ´úÂ룺
@extends(‘users.show’) @section(‘title’, ‘Óû§Áбí’) @section(‘content’)
Óû§Áбí
Taylor
Dayle
@endsection
ÕâÀïÎÒÃÇʹÓÃÁË@extendsÖ¸ÁîÀ´¼ÌÐøusers.showÊÓͼ£¬²¢ÇÒʹÓÃ@sectionÖ¸ÁîÀ´½ç˵ÁË×Ô¼ºµÄtitleºÍcontentÇøÓòÄÚÈÝ¡£ÆäÖУ¬contentÇøÓòÖаüÀ¨ÁËÒ»¸öulÁÐ±í£¬ÓÃÓÚչʾÏêϸµÄÓû§ÐÅÏ¢¡£
5¡¢Ê¹ÓÃORMÖÎÀíÊý¾Ý¿â
ÔÚLaravel¿ò¼ÜÖУ¬¿ÉÒÔʹÓÃORMÀ´ÖÎÀíÊý¾Ý¿â¡£ÀýÈ磬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´½¨ÉèÒ»¸öUserÄ£×Ó£º
php artisan make:model User
Õ⽫»áÔÚappĿ¼Ï½¨ÉèÒ»¸öÃûΪUserµÄPHPÎļþ£¬¿ÉÒÔÔÚ¸ÃÎļþÖнç˵¸ÃÄ£×ӺͱíÖ®¼äµÄÓ³Éä¹Øϵ¡£
ÔÚLaravel¿ò¼ÜÖУ¬¿ÉÒÔʹÓÃEloquentÄ£×ÓÀ´ºÍÊý¾Ý¿âʵÏÖ½»»¥¡£ÀýÈ磺
// ÅÌÎÊËùÓÐÓû§ $users = AppUser::all(); // ÅÌÎÊÃû³ÆΪTaylorµÄÓû§ $user = AppUser::where(‘name’, ‘Taylor’)->first();
ÔÚÕâÑùµÄÅÌÎÊÖУ¬LaravelʹÓÃÒ»¸öºÜÊǾ«Á·ÇÒÒ×ÓÚÃ÷È·µÄAPIÀ´ÊµÏÖORM¡£Í¬Ê±£¬¿ÉÒÔʹÓøü¶àÖØ´óµÄÅÌÎʲÙ×÷£¬ºÃ±ÈÁ´Ê½Å²ÓᢾۺϺÍÅþÁ¬µÈµÈ¡£
ËÄ¡¢½áÂÛ
ÔÚ±¾ÎÄÖУ¬ÎÒÃǼòÒªÏÈÈÝÁËLaravel¿ò¼Ü£¬²¢ÇÒ̽ÌÖÁËÔõÑùʹÓÃLaravel¿ò¼ÜÀ´´î½¨¸ßÖÊÁ¿µÄWebÓ¦ÓóÌÐò¡£Laravel¿ò¼ÜÊÇÒ»¸öÇ¿Ê¢ÇÒÒ×ÓÚʹÓõĿò¼Ü£¬ËüÓµÓÐÎÞаµÄ½á¹¹ºÍÇ¿Ê¢µÄ¹¦Ð§£¬¿ÉÒÔÇáËɵØÖª×㿪·¢ÕßµÄÐèÇó¡£ÈôÊÇÄúÕýÔÚÑ°ÕÒÒ»¸ö¸ßЧµÄPHP Web¿ò¼ÜÀ´¼ÓËÙÄúµÄWebÓ¦ÓóÌÐòµÄ¿ª·¢Àú³Ì£¬ÄÇôLaravel¿ò¼ÜÒ»¶¨ÊÇÄúµÄ²»¶þ֮ѡ¡£
ÒÔÉϾÍÊÇʹÓÃlaravelÀ´´î½¨phpµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡