laravelÏîÄ¿ÔõÑùÆô¶¯
laravelÊÇÒ»¿î»ùÓÚphpÓïÑԵĿªÔ´webÓ¦Óÿò¼Ü£¬ÆÕ±éÓ¦ÓÃÓÚwebÓ¦ÓõĿª·¢¡£ËüÒÔ¾«Á·¡¢ÓÅÑÅ¡¢¸»ÓÚÌåÏÖÁ¦µÄÓï·¨ºÍ¶Ôweb¿ª·¢Àú³ÌµÄÖ§³Ö¶øÖøÃû£¬²¢ÇÒÓµÓÐÖØ´óµÄÉçÇøºÍ¸»ºñµÄÉú̬ϵͳ£¬Òò´Ë»ñµÃÁË¿ª·¢ÕßÃǵÄÇàíù¡£
ÔÚLaravelµÄ¹Ù·½ÍøÕ¾ÉÏ£¬ÎÒÃÇ¿ÉÒÔºÜÈÝÒ×µØÕÒµ½×°ÖÃLaravelµÄÏêϸÎĵµºÍ°ì·¨£¬ÏÂÃæÎÒÃǽ«¼òÒªÏÈÈÝÒ»ÏÂÔõÑùÆô¶¯LaravelÏîÄ¿¡£
×°ÖÃComposer
LaravelʹÓÃComposerÖÎÀíÒÀÀµÏÒò´ËÎÒÃÇÐèÒªÏÈ×°ÖÃComposer¡£ÔÚÏÂÁîÐÐÖÐÊäÈëÒÔÏÂÏÂÁî×°Öãº
curl -sS https://getcomposer.org/installer | php sudo mv composer.phar /usr/local/bin/composer
µÇ¼ºó¸´ÖÆ
×°ÖÃÍê³Éºó£¬ÎÒÃÇ¿ÉÒÔÊäÈëÒÔÏÂÏÂÁî¼ì²éÊÇ·ñ×°ÖÃÀֳɣº
composer
µÇ¼ºó¸´ÖÆ
×°ÖÃLaravel
ÔÚ×°ÖÃLaravel֮ǰ£¬ÎÒÃÇÐèÒªÏȽ¨ÉèÒ»¸öеÄÄ¿ÈÎÃüÓÚÉúÑÄÏîÄ¿Îļþ¡£ÔÚÏÂÁîÐÐÖÐÖ´ÐÐÒÔÏÂÏÂÁ
mkdir mylaravelapp cd mylaravelapp
µÇ¼ºó¸´ÖÆ
È»ºóÎÒÃÇ¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁî×°ÖÃ×îа汾µÄLaravel£º
composer create-project --prefer-dist laravel/laravel .
µÇ¼ºó¸´ÖÆ
ÕâÀïµÄµãÌåÏÖÎÒÃǽ«Laravel×°ÖÃÔÚÄ¿½ñĿ¼Ï¡£
×°ÖÃÍê³Éºó£¬ÔÚÏÂÁîÐÐÖÐÊäÈëÒÔÏÂÏÂÁîÆô¶¯LaravelÏîÄ¿£º
php artisan serve
µÇ¼ºó¸´ÖÆ
Õ⽫Æô¶¯Ò»¸öÍâµØ¿ª·¢Ð§ÀÍÆ÷£¬²¢ÔÚÏÂÁîÐÐÖÐÊä³öÈçÏÂÐÅÏ¢£º
Laravel development server started: http://127.0.0.1:8000
µÇ¼ºó¸´ÖÆ
ÎÒÃÇ¿ÉÒÔÔÚä¯ÀÀÆ÷ÖÐÊäÈëÕâ¸öµØµã£¬¾Í¿ÉÒÔ¿´µ½LaravelµÄ½Ó´ý½çÃæÁË¡£
ÉèÖÃÊý¾Ý¿â
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒªÉèÖÃÊý¾Ý¿â£¬ÒÔ±ãÎÒÃǵÄLaravelÓ¦ÓóÌÐòÄܹ»ÅþÁ¬Êý¾Ý¿â²¢Ê¹ÓÃËü¡£ÔÚmylaravelapp¸ùĿ¼ÏÂÕÒµ½.envÎļþ£¬ÐÞ¸ÄÒÔϱäÁ¿Öµ£º
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=mylaravelapp DB_USERNAME=root DB_PASSWORD=
µÇ¼ºó¸´ÖÆ
ÕâÀÎÒÃÇʹÓÃÁËMySQL×÷ΪÊý¾Ý¿â£¬Êý¾Ý¿âÃû³ÆΪmylaravelapp£¬Óû§ÃûΪroot£¬ÃÜÂëΪ¿Õ¡£
È»ºó£¬ÔÚÏÂÁîÐÐÖÐÖ´ÐÐÒÔÏÂÏÂÁîÌìÉúLaravelÓ¦ÓóÌÐòÃÜÔ¿£º
php artisan key:generate
µÇ¼ºó¸´ÖÆ
×îºó£¬ÔÚÏÂÁîÐÐÖÐÖ´ÐÐÒÔÏÂÏÂÁîÖ´ÐÐÊý¾Ý¿âǨá㣺
php artisan migrate
µÇ¼ºó¸´ÖÆ
Õ⽫½¨ÉèÊý¾Ý¿â±íºÍ×ֶΣ¬²¢Æ¾Ö¤ÎÒÃÇÔÚÓ¦ÓóÌÐòÖнç˵µÄÄ£×ÓÀཨÉè¶ÔÓ¦µÄ¹Øϵ¡£
½¨Éè·ÓɺÍÊÓͼ
ÏÖÔÚÎÒÃǵÄLaravelÓ¦ÓóÌÐòÒѾ¿ÉÒÔÅþÁ¬Êý¾Ý¿â²¢×¼±¸ºÃʹÓÃËüÁË£¬½ÓÏÂÀ´ÎÒÃÇ¿ÉÒÔ½¨ÉèһЩ·ÓɺÍÊÓͼÀ´Õ¹Ê¾Êý¾Ý¡£
ÔÚapp/Http/ControllersĿ¼Ï½¨ÉèÒ»¸öеĿØÖÆÆ÷£º
php artisan make:controller UserController
µÇ¼ºó¸´ÖÆ
È»ºóÔÚUserControllerÀàÖн¨ÉèÒ»¸öindexÒªÁ죺
public function index() { $users = App\User::all(); return view('users', ['users' => $users]); }
µÇ¼ºó¸´ÖÆ
Õâ¸öÒªÁ콫»ñÈ¡ËùÓÐÓû§Êý¾Ý£¬²¢½«Æäת´ïµ½ÃûΪusers.blade.phpµÄÊÓͼÖС£
½¨ÉèÒ»¸öеķÓÉ£¬ÔÚroutes/web.phpÎļþÖÐÌí¼ÓÈçÏ´úÂ룺
Route::get('/users', 'UserController@index');
µÇ¼ºó¸´ÖÆ
×îºó£¬ÔÚresources/viewsĿ¼Ï½¨ÉèÒ»¸öÃûΪusers.blade.phpµÄÊÓͼÎļþ£º
@foreach ($users as $user) <p>{{ $user->name }}</p> @endforeach
µÇ¼ºó¸´ÖÆ
ÏÖÔÚ£¬ÔÚä¯ÀÀÆ÷ÖÐÊäÈëhttp://127.0.0.1:8000/users£¬Äú½«»á¿´µ½ËùÓÐÓû§µÄÃû³Æ±»Áгö¡£
×ܽá
Æô¶¯LaravelÏîÄ¿ÐèÒª¾ÙÐÐÒÔÏ°취£º
×°ÖÃComposer
×°ÖÃLaravel
ÉèÖÃÊý¾Ý¿â
½¨Éè·ÓɺÍÊÓͼ
ͨ¹ýÕâЩ°ì·¨£¬ÎÒÃǾͿÉÒÔ½¨ÉèÆðÒ»¸ö¼òÆÓµÄLaravelÓ¦ÓóÌÐò£¬ÒÔչʾÊý¾Ý²¢ÊµÏÖ»ù±¾µÄWeb¿ª·¢¡£ËäÈ»£¬ÕâÖ»ÊÇÒ»¸ö¼òÆÓµÄÆðµã£¬LaravelÉÐÓиü¶àµÄ¹¦Ð§ºÍÌØÕ÷£¬¿ÉÒÔÈÃÎÒÃÇ¿ª·¢Ô½·¢¸»ºñ¡¢ÖØ´óµÄÓ¦ÓóÌÐò¡£
ÒÔÉϾÍÊÇlaravelÏîÄ¿ÔõÑùÆô¶¯µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡