laravelÔõô¿ìËٴ
laravelÊÇÒ»¿îÊ¢ÐеÄphp¿ò¼Ü£¬ËüÌṩÁ˱ã½ÝµÄ¿ª·¢¹¤¾ßºÍÓÅÒìµÄÐÔÄÜÌåÏÖ£¬³ÉΪÁËÐí¶àweb¿ª·¢ÕßµÄÊ×Ñ¡¡£¹ØÓÚÏëҪѸËÙѧϰºÍ´î½¨laravelÏîÄ¿µÄ¿ª·¢ÕßÀ´Ëµ£¬½ÓÏÂÀ´µÄÕâƪÎÄÕ½«ÎªÄãÌṩһЩ»ù±¾µÄÖ¸µ¼¡£
ÇéÐδ
ÔÚ×îÏÈʹÓÃLaravel֮ǰ£¬È·±£ÄãµÄµçÄÔÒѾװÖÃÁËPHPºÍComposerÕâÁ½¸ö¹¤¾ß¡£PHPÊÇLaravelÒÀÀµµÄ½¹µãÓïÑÔ£¬¶øComposerÔòÊÇPHPµÄÒÀÀµÖÎÀíÆ÷¡£ÔÚ×°ÖÃComposerºó£¬¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁîÀ´×°ÖÃLaravel£º
composer global require laravel/installer
µÇ¼ºó¸´ÖÆ
×°ÖÃÍê³Éºó£¬¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁî¼ì²éÊÇ·ñ×°ÖÃÀֳɣº
laravel --version
µÇ¼ºó¸´ÖÆ
½¨ÉèÏîÄ¿
ÔÚ×°ÖÃÍê³ÉLaravelºó£¬¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁîÀ´½¨ÉèÒ»¸öеÄLaravelÏîÄ¿£º
laravel new my-project
µÇ¼ºó¸´ÖÆ
ÕâÀïmy-project¾ÍÊÇÄãÏëÒª½¨ÉèµÄÏîÄ¿µÄÃû³Æ¡£Laravel½«»áÔÚÕâ¸öĿ¼Ï½¨ÉèÒ»¸öеÄÏîÄ¿¡£
Ò²¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´½¨ÉèÒ»¸öLaravelÏîÄ¿£º
composer create-project --prefer-dist laravel/laravel my-project
µÇ¼ºó¸´ÖÆ
ÕâÖÖ·½·¨»áͨ¹ýComposer´ÓPackagistÉÏ×°ÖÃLaravel£¬È»ºóÔÚmy-projectĿ¼ÀィÉèÒ»¸öÐÂÏîÄ¿¡£
Æô¶¯ÏîÄ¿
LaravelÏîÄ¿½¨ÉèÍê³Éºó£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´Æô¶¯ÏîÄ¿£º
php artisan serve
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁ»áÔÚÍâµØÆô¶¯Ò»¸öWebЧÀÍÆ÷²¢¼àÌý8000¶Ë¿Ú¡£ÔÚä¯ÀÀÆ÷ÖÐÊäÈëhttp://localhost:8000¾Í¿ÉÒÔ»á¼ûÕ¾µã¡£
ÉèÖÃÊý¾Ý¿â
LaravelĬÈÏʹÓÃSQLiteÊý¾Ý¿â£¬¿ÉÊǵ±ÎÒÃÇÐèҪʹÓÃÆäËûÀàÐ͵ÄÊý¾Ý¿âʱ£¬ÐèÒª¾ÙÐÐÏìÓ¦µÄÉèÖá£ÔÚLaravelÖУ¬Êý¾Ý¿âÉèÖÃÎļþλÓÚconfig/database.phpÖС£ÎÒÃÇ¿ÉÒÔÔÚÕâÀï¾ÙÐÐÊý¾Ý¿âÉèÖá£
ÔÚconfig/database.phpÖпÉÒÔ¿´µ½ÀàËÆÕâÑùµÄÉèÖã¬ÕâÊÇĬÈϵÄSQLiteÉèÖãº
'sqlite' => [ 'driver' => 'sqlite', 'url' => env('DATABASE_URL'), 'database' => env('DB_DATABASE', database_path('database.sqlite')), 'prefix' => '', 'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true), ],
µÇ¼ºó¸´ÖÆ
ÎÒÃÇ¿ÉÒÔ½«Õâ¸öÉèÖÃÌæ»»³ÉÆäËûÀàÐÍÊý¾Ý¿âµÄÉèÖ㬺ñÈMySQL¡£ºÃ±È£¬ÒÔÏÂÊÇÒ»¸öMySQLÉèÖãº
'mysql' => [ 'driver' => 'mysql', 'url' => env('DATABASE_URL'), 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'laravel'), 'username' => env('DB_USERNAME', 'root'), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'prefix_indexes' => true, 'strict' => true, 'engine' => null, 'options' => extension_loaded('pdo_mysql') ? array_filter([ PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), ]) : [], ],
µÇ¼ºó¸´ÖÆ
ÒÔÉÏÉèÖÃÖУ¬ÐèҪעÖØÐ޸ĵÄ×ֶΰüÀ¨host¡¢port¡¢database¡¢usernameºÍpassword£¬ÕâЩ×Ö¶ÎÐèÒªÉèÖóÉÄã×Ô¼ºµÄÊý¾Ý¿âÉèÖá£
½¨Éè¿ØÖÆÆ÷ºÍÊÓͼ
ÔÚLaravelÖн¨ÉèÒ»¸ö¿ØÖÆÆ÷¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁîÀ´Íê³É£º
php artisan make:controller HomeController
µÇ¼ºó¸´ÖÆ
ÕâÀィÉèÁËÒ»¸ö½Ð×öHomeControllerµÄ¿ØÖÆÆ÷¡£¿ØÖÆÆ÷µÄ×÷ÓÃÊǽÓÊÜÓû§µÄÇëÇó²¢·µ»ØÏìÓ¦¡£
µ±¿ØÖÆÆ÷ÖÐÐèÒªÇл»ÊÓͼʱ£¬¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁîÀ´½¨ÉèÊÓͼÎļþ£º
php artisan make:view home
µÇ¼ºó¸´ÖÆ
ÕâÀィÉèÁËÒ»¸öÃû×ÖΪhomeµÄÊÓͼ£¬¿ÉÒÔÔÚHomeControllerÖÐͨ¹ýÒÔÏ´úÂëÀ´äÖȾhomeÊÓͼ£º
return view('home');
µÇ¼ºó¸´ÖÆ
ÔÚweb.phpÎļþÖпÉÒÔʹÓÃÒÔÏ´úÂëÀ´°ó¶¨URLºÍ¿ØÖÆÆ÷£º
Route::get('/', 'HomeController@index')->name('home');
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂ뽫Ê×Ò³(/)ºÍHomeControllerÖеÄindexÒªÁì°ó¶¨ÔÚÒ»Æ𣬲¢Ê¹ÓÃhome×÷ΪÃû³Æ¡£½ÓÏÂÀ´£¬ÔÚ¿ØÖÆÆ÷ÖÐÌí¼ÓindexÒªÁ죺
public function index() { return view('home'); }
µÇ¼ºó¸´ÖÆ
ÕâÀïÊÇÒ»¸ö¼òÆÓµÄHomeController´úÂ룬Ëü·µ»ØÁËÒ»¸öhomeÊÓͼ£º
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class HomeController extends Controller { public function index() { return view('home'); } }
µÇ¼ºó¸´ÖÆ
½áÂÛ
±¾ÎÄÌṩÁ˹ØÓÚ¿ìËٴlaravelÏîÄ¿µÄ½Ìѧ£¬Ï£ÍûÄܹ»×ÊÖúµ½²¿·ÖÏëѧϰlaravel¿ò¼Ü£¬²¢ÇҴÊôÓÚ×Ô¼ºlaravelÏîÄ¿µÄ¿ª·¢Õß¡£ËäÈ»£¬ÔÚÏÖʵ¿ª·¢ÖУ¬»¹ÐèÒª¸üÉîÈëµÄѧϰºÍÏàʶ£¬²Å»ª¸üºÃµØʹÓÃLaravel¾ÙÐпª·¢¡£
ÒÔÉϾÍÊÇlaravelÔõô¿ìËٴµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡