ÔõÑùʹÓÃLaravel¿ò¼ÜÀ´¹¹½¨WebÓ¦ÓóÌÐò
laravelÊÇÒ»¸ö¹¦Ð§Ç¿Ê¢µÄphp¿ò¼Ü£¬ËüÌṩÁËһϵÁеŤ¾ßºÍ×é¼þ£¬ÉÁ¿ª·¢Õß¿ÉÒÔ¿ìËÙ¡¢¸ßЧµØ¹¹½¨webÓ¦ÓóÌÐò¡£ÔÚ¿ª·¢webÓ¦ÓóÌÐòµÄÀú³ÌÖУ¬ÄúÐèÒªÕÆÎÕlaravel¿ò¼ÜµÄ½¹µã¿´·¨ºÍ֪ʶ£¬Í¬Ê±Ê¹ÓÃһЩ³£ÓõŤ¾ßºÍÊÖÒÕ¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃlaravel¿ò¼ÜÀ´¹¹½¨webÓ¦ÓóÌÐò¡£
×°ÖÃLaravel
Ê×ÏÈ£¬ÄúÐèÒªÔÚÍâµØÅÌËã»úÉÏ×°ÖÃLaravel¡£Äú¿ÉÒÔʹÓÃComposerÀ´×°ÖÃLaravel¡£ComposerÊÇPHPµÄÒ»¸öÒÀÀµÖÎÀíÆ÷£¬Ëü¿ÉÒÔ×°ÖᢸüкÍÖÎÀíPHP°ü¡£ÏÂÃæÊÇ×°ÖÃLaravelµÄ°ì·¨£º
·¿ªÖն˻òÏÂÁîÐй¤¾ß
ÔËÐÐÒÔÏÂÏÂÁcomposer global require laravel/installer
ÔËÐÐÒÔÏÂÏÂÁlaravel new your_project_name
½øÈëÄúµÄÏîĿĿ¼£ºcd your_project_name
ÔËÐÐLaravel¿ª·¢Ð§ÀÍÆ÷£ºphp artisan serve
½¨Éè·ÓÉ
ÔÚLaravelÖУ¬Â·ÓÉÓÃÓÚ½ç˵ÇëÇóURIºÍHTTPÇëÇóÐж¯Ö®¼äµÄÓ³Éä¡£Äú¿ÉÒÔͨ¹ý½¨Éè·ÓÉÀ´´¦ÀíHTTPÇëÇó²¢·µ»ØÏìÓ¦¡£LaravelÖеÄ·ÓÉϵͳºÜÊǼòÆÓ£¬ÄúÖ»ÐèÒª½ç˵ÇëÇóURIºÍHTTPÇëÇóÐж¯¼´¿É¡£ÀýÈ磬ÒÔÏ·ÓÉÉùÃ÷½«´¦ÀíGETÇëÇó²¢·µ»ØHello World£º
Route::get(‘/’, function () {
return 'Hello World';
µÇ¼ºó¸´ÖÆ
});
½¨Éè¿ØÖÆÆ÷
ÔÚLaravelÖУ¬¿ØÖÆÆ÷ÓÃÓÚ´¦ÀíHTTPÇëÇó²¢ÌìÉúÏìÓ¦¡£Äú¿ÉÒÔͨ¹ý½¨Éè¿ØÖÆÆ÷À´½«ÇëÇóÁ÷³ÌÊèɢΪ²î±ðµÄ²Ù×÷¡£ÒÔÏ´úÂëÑÝʾÁËÔõÑù½¨ÉèÒ»¸öÃûΪUserControllerµÄ¿ØÖÆÆ÷£º
php artisan make:controller UserController
È»ºó£¬Äú¿ÉÒÔʹÓÃÒÔÏÂÒªÁìÌí¼Ó²Ù×÷£º
public function index()
{
// Some logic here
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
}
public function show($id)
{
// Some logic here
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
}
½¨ÉèÊÓͼ
ÔÚLaravelÖУ¬¿ÉÒÔʹÓÃÊÓͼÀ´ÌìÉúWeb½çÃæ»òäÖȾHTML¡£ÊÓͼÊÇÒ»¸ö¼òÆÓµÄHTMLÎļþ£¬ÆäÖаüÀ¨¶¯Ì¬ÌìÉúµÄÊý¾Ý¡£Äú¿ÉÒÔʹÓÃBladeÄ£°åÒýÇæÀ´´¦ÀíÊÓͼ¡£ÒÔÏÂÊÇÔõÑù½¨ÉèÒ»¸öÊÓͼµÄ´úÂ룺
php artisan make:view welcome
½ÓÏÂÀ´£¬Äú¿ÉÒÔÔÚÒÔϵصãÕÒµ½Äú½¨ÉèµÄÊÓͼ£ºresources/views/welcome.blade.php¡£
ÓëÊý¾Ý¿â½»»¥
ÔÚLaravelÖУ¬Äú¿ÉÒÔʹÓÃEloquent ORMÓëÊý¾Ý¿â½»»¥¡£Eloquent ORMÊÇÒ»¸ö¼òÆÓµÄActive RecordʵÏÖ£¬Ëü¿ÉÒÔÈÃÄúºÜÊÇÀû±ãµØ´ÓÊý¾Ý¿âÖмìË÷¡¢Ìí¼Ó¡¢¸üкÍɾ³ý¼Í¼¡£ÒÔÏ´úÂëÑÝʾÁËÔõÑùʹÓÃEloquent ORM´Óusers±íÖмìË÷ËùÓмͼ£º
$users = User::all();
½¨ÉèǨáã
ÔÚLaravelÖУ¬Äú¿ÉÒÔʹÓÃǨáãÀ´ÖÎÀíÊý¾Ý¿â¼Ü¹¹¡£Ç¨áãÊÇÒ»ÖÖ»úÖÆ£¬¿ÉÒÔÈÃÄúÇáËɵظú×ÙºÍÐÞ¸ÄÊý¾Ý¿â½á¹¹¡£Äú¿ÉÒÔʹÓÃÒÔÏÂÏÂÁÉèÒ»¸öÃûΪusersµÄǨáãÎļþ£º
php artisan make:migration create_users_table –create=users
¸ÃÏÂÁÌìÉúÒ»¸öǨáãÎļþ£¬Äú¿ÉÒԱ༸ÃÎļþÒÔ½ç˵ҪִÐеIJÙ×÷£¬ÀýÈ罨Éè»òɾ³ý±í¡£ÒÔÏÂÊÇÈç×÷Éõusers±í½¨ÉèÒ»¸öǨáãµÄ´úÂ룺
public function up()
{
Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); });
µÇ¼ºó¸´ÖÆ
}
ʹÓÃÖÐÐļþ
ÔÚLaravelÖУ¬Äú¿ÉÒÔʹÓÃÖÐÐļþÀ´´¦ÀíHTTPÇëÇó¡£ÖÐÐļþÊÇÒ»ÖÖ»úÖÆ£¬¿ÉÒÔÈÃÄúÔÚÇëÇóµÖ´ïÄúµÄÓ¦ÓóÌÐò֮ǰ»òÖ®ºóÖ´ÐвÙ×÷¡£ÀýÈ磬Äú¿ÉÒÔ½¨ÉèÒ»ÆäÖÐÐļþÀ´ÑéÖ¤Óû§ÊÇ·ñÒѵǼ£¬ÈôÊÇûÓеǼ£¬ÔòÐèÒª½«Óû§Öض¨Ïòµ½µÇ¼ҳÃæ¡£ÒÔÏ´úÂëÊÇÔõÑù½¨É貢ʹÓÃÒ»¸öÃûΪAuthenticateµÄÖÐÐļþµÄʾÀý£º
php artisan make:middleware Authenticate
public function handle($request, Closure $next)
{
if (! $request->user()) { return redirect('/login'); } return $next($request);
µÇ¼ºó¸´ÖÆ
}
¼¯³ÉµÚÈý·½Ð§ÀÍ
ÔÚLaravelÖУ¬Äú¿ÉÒÔÇáËɵؼ¯³ÉÖÖÖÖµÚÈý·½Ð§ÀÍ£¬ÀýÈçÖ§¸¶Íø¹Ø¡¢ÓʼþЧÀÍ¡¢ÍÆËÍ֪ͨЧÀ͵ȡ£Äú¿ÉÒÔʹÓÃLaravelµÄ¹Ù·½°ü»ò¿ª·¢µÄÀ©Õ¹À´¿ìËÙÍê³É´ËʹÃü¡£ÀýÈ磬ÒÔÏ´úÂëÑÝʾÁËÔõÑùʹÓÃLaravelµÄ¹Ù·½°ülaravel/cashierÀ´¼¯³ÉStripeÖ§¸¶Íø¹Ø£º
composer require laravel/cashier
»º´æÕ½ÂÔ
ÔÚLaravelÖУ¬Äú¿ÉÒÔʹÓûº´æÀ´Ìá¸ßWebÓ¦ÓóÌÐòµÄÐÔÄÜ¡£LaravelÌṩÁËÐí¶à»º´æÇý¶¯³ÌÐò£¬ÀýÈçRedis¡¢MemcachedµÈ¡£Äú¿ÉÒÔʹÓÃÒÔÏ´úÂëƬ¶ÏÀ´½ç˵»º´æÕ½ÂÔ£º
Cache::remember(‘users’, $minutes, function () {
return DB::table('users')->get();
µÇ¼ºó¸´ÖÆ
});
ʹÓÃÐÐÁÐ
ÔÚLaravelÖУ¬Äú¿ÉÒÔʹÓÃÐÐÁÐÀ´´¦ÀíºÄʱºÍÐèÒªÒì²½Ö´ÐеÄʹÃü¡£ÐÐÁÐÊÇÒ»ÖÖ»úÖÆ£¬¿ÉÒÔÈÃÄú½«Ê¹Ãü·ÅÈëÐÐÁÐÖУ¬²¢ÔÚ´¦Àíʱ¼ä½Ï³¤µÄʹÃüʱ²»ÛÕ±ÕÖ÷Ó¦ÓóÌÐòÏ̡߳£Äú¿ÉÒÔʹÓÃĬÈÏÐÐÁÐÇý¶¯³ÌÐò»ò½«Ê¹Ãü·¢ÍùRedis¡¢BeanstalkdµÈЧÀÍ¡£ÒÔÏÂÊÇÔõÑù½«Ê¹ÃüÍÆÈëÐÐÁеÄʾÀý´úÂ룺
dispatch(function () {
// Some long-running code here
µÇ¼ºó¸´ÖÆ
});
×ܽá
ÒÔÉÏÊÇʹÓÃLaravel¿ª·¢WebÓ¦ÓóÌÐòµÄ»ù±¾°ì·¨¡£ËäÈ»£¬LaravelÌṩÁ˸ü¶àµÄ¹¦Ð§ºÍ¹¤¾ß£¬ÈÃÄú¿ÉÒÔÔ½·¢¸ßЧ¡¢ÎÞаµØ¿ª·¢WebÓ¦ÓóÌÐò¡£ÕÆÎÕÕâЩ½¹µã¿´·¨ºÍÊÖÒÕ£¬Äú½«Äܹ»¹¹½¨³ö¸ßÖÊÁ¿¡¢¸ßÐÔÄܵÄWebÓ¦ÓóÌÐò¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃLaravel¿ò¼ÜÀ´¹¹½¨WebÓ¦ÓóÌÐòµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡