laravel¿ò¼ÜÔõôÌìÉú
laravel¿ò¼ÜÔõôÌìÉú
Ëæ×ÅWebÓ¦ÓóÌÐòµÄÐèÇóÒ»Ö±ÔöÌí£¬Ê¹Óÿò¼ÜÀ´½¨ÉèÈí¼þÒ²±äµÃÔ½À´Ô½Ê¢ÐС£Laravel¿ò¼ÜÊÇÒ»ÖÖÊ¢ÐеÄWebÓ¦ÓóÌÐò¿ò¼Ü£¬ËüÌṩÁËÐí¶àÏÖ´ú¿ª·¢¹¤¾ßºÍÊÖÒÕ£¬¿É×ÊÖúÄú¿ìËÙ¹¹½¨ÖØ´óµÄWebÓ¦ÓóÌÐò¡£
±¾ÎĽ«ÏòÄúÏÈÈÝLaravel¿ò¼ÜµÄÌìÉúÀú³Ì¡£ÎÒÃǽ«´ÓLaravelµÄ×°Öõ½ÌìÉú³õʼӦÓóÌÐò£¬ÔÙÏÈÈÝÔõÑù½¨ÉèÊý¾Ý¿âǨáãÒÔ¼°»ù±¾µÄ¿ØÖÆÆ÷ºÍÊÓͼÎļþ¡£
Ò»¡¢×°ÖÃLaravel
ҪװÖÃLaravel£¬ÄúÐèÒªÔÚÅÌËã»úÉÏ×°ÖÃPHPºÍComposer¡£ÔÚ´ËÖ®ºó£¬¿ÉÒÔʹÓÃComposerÀ´×°ÖÃLaravel¡£
ʹÓÃComposer×°ÖÃLaravel
·¿ªÖնˣ¨»òÏÂÁîÌáÐÑ·û£©£¬²¢µ¼º½µ½ÄúҪװÖÃLaravelµÄĿ¼¡£ÔÚ¸ÃĿ¼Ï£¬¼üÈëÒÔÏÂÏÂÁ
composer create-project laravel/laravel my-projec
Õâ»áÏÂÔØLaravel¿ò¼Ü²¢½¨ÉèÒ»¸öÃûΪmy-projectµÄĿ¼¡£¸ÃĿ¼°üÀ¨LaravelµÄËùÓÐÎļþºÍĿ¼£¬ÒÔ¼°°üÀ¨ÓÃÓÚ¹¹½¨Ó¦ÓóÌÐòËùÐèµÄÆäËûÒÀÀµÏî¡£
¶þ¡¢ÌìÉú³õʼӦÓóÌÐò
ÀÖ³É×°ÖÃLaravelºó£¬Äú¿ÉÒÔʹÓÃLaravelµÄÏÂÁîÐй¤¾ßÌìÉúLaravelÓ¦ÓóÌÐòµÄ³õʼ½á¹¹¡£
ÌìÉú³õʼӦÓóÌÐò
·¿ªÖնˣ¬²¢µ¼º½µ½ÒÑ×°ÖÃLaravelµÄĿ¼¡£ÔÚÏÂÁîÐÐÖÐÊäÈëÒÔÏÂÏÂÁ
php artisan make:auth
Õ⽫½¨ÉèÉí·ÝÑéÖ¤½çÃæ²¢ÌìÉú¿ØÖÆÆ÷¡¢ÊÓͼºÍ·ÓɵȱØÐèµÄÎļþºÍĿ¼¡£
ÔËÐÐÓ¦ÓóÌÐò
ÔËÐÐLaravelÓ¦ÓóÌÐò£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁ
php artisan serve
´ËÏÂÁÆô¶¯¿ª·¢WebЧÀÍÆ÷£¬¿ÉÒÔÔÚä¯ÀÀÆ÷Öлá¼ûÓ¦ÓóÌÐò¡£
Èý¡¢½¨ÉèÊý¾Ý¿âǨáã
ÔÚLaravelÖУ¬Äú¿ÉÒÔʹÓÃǨáãÀ´ÖÎÀíÊý¾Ý¿âµÄÊý¾Ý½á¹¹¡£Êý¾Ý¿âǨáãÊÇÒ»ÖÖ¿ÉÖظ´µÄÒªÁ죬¿ÉÒÔʹÄúÇáËÉÌí¼Ó¡¢Ð޸ĺ͵õÏúÊý¾Ý¿â±íµÄ½á¹¹¡£
½¨ÉèеÄǨáã
Ϊ½¨ÉèеÄÊý¾Ý¿âǨá㣬ÇëÔÚÖÕ¶ËÖÐÊäÈëÒÔÏÂÏÂÁ
php artisan make:migration create_posts_table
´ËÏÂÁÔÚLaravelÊý¾Ý¿âǨáãĿ¼ÖеÄmigrationsĿ¼Öн¨ÉèÒ»¸öÐÂÎļþ13λÃüÃû¼Æ»®µÄÎļþ¡£
ÐÞ¸ÄǨáã
·¿ªÇ¨áãÎļþ²¢Ê¹ÓÃLaravelÌṩµÄSchemaÀà½ç˵Ҫ½¨ÉèµÄ¹¤¾ß¡£ÀýÈ磬ÒÔÏÂÊǽ¨ÉèÃûΪ”posts”µÄ±íµÄʾÀý£º
use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
class CreatePostsTable extends Migration
{
public function up() { Schema::create('posts', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->text('body'); $table->timestamps(); }); } public function down() { Schema::dropIfExists('posts'); }
µÇ¼ºó¸´ÖÆ
}
´ËǨá㽫ÔÚÊý¾Ý¿âÖн¨ÉèÒ»¸öÃûΪ”posts”µÄ±í£¬²¢°üÀ¨×ֶΔid”£¬”title”£¬”body”ÒÔ¼°Êý¾Ý¿âµÄĬÈÏʱ¼ä´Á×ֶΡ£
ÔËÐÐǨáã
ÒªÓ¦ÓÃÕâ¸öǨáã²¢½¨ÉèÏìÓ¦µÄ±í£¬ÇëÔÚÖÕ¶ËÖÐÔËÐÐÒÔÏÂÏÂÁ
php artisan migrate
Õ⽫½¨ÉèÖ¸¶¨µÄ±í¡£
ËÄ¡¢½¨Éè»ù±¾¿ØÖÆÆ÷ºÍÊÓͼ
½¨Éè¿ØÖÆÆ÷
ʹÓÃÒÔÏÂÏÂÁîÔÚÖÕ¶ËÖн¨ÉèÒ»¸öеĿØÖÆÆ÷£º
php artisan make:controller PostController
Õ⽫½¨ÉèÒ»¸öеÄPostController.phpÎļþ£¬¸ÃÎļþλÓÚapp/Http/ControllersĿ¼ÖС£
¿ØÖÆÆ÷±àд
ÔÚеÄPostController.phpÎļþÖУ¬½«ÒÔÏÂÄÚÈÝÌí¼Óµ½Îļþ¿ªÍ·£º
namespace AppHttpControllers;
use IlluminateHttpRequest;
class PostController extends Controller
{
//
µÇ¼ºó¸´ÖÆ
}
´Ë´úÂëµ¼ÈëLaravelµÄ¿ØÖÆÆ÷À࣬²¢½¨ÉèÒ»¸öÃûΪPostControllerµÄ¿ØÖÆÆ÷Àà¡£
Ìí¼ÓÒÔÏÂÒªÁìÒÔ´¦Àí¿ØÖÆÆ÷µÄ²Ù×÷£º
public function index()
{
return view('posts.index');
µÇ¼ºó¸´ÖÆ
}
´ËÒªÁ콫·µ»ØÒ»¸öÃûΪ”index”µÄÊÓͼ£¬²¢ÓÉÊÓͼÎļþ¿ØÖÆ¡£
½¨ÉèÊÓͼ
ʹÓÃÒÔÏÂÏÂÁîÔÚÖÕ¶ËÖн¨ÉèÒ»¸öеÄÊÓͼ£º
php artisan make:view posts.index
Õâ»á½¨ÉèÒ»¸öÃûΪ”index.blade.php”µÄÊÓͼÎļþ¡£¸ÃÎļþλÓÚresources/views/postsĿ¼ÖС£
ÊÓͼ±àд
ÔÚн¨ÉèµÄ”index.blade.php”ÎļþÖУ¬Ìí¼ÓÒÔÏÂÄÚÈÝ£º
@extends(‘layouts.app’)
@section(‘content’)
<h1>All Posts</h1>
µÇ¼ºó¸´ÖÆ
@endsection
´ËÊÓͼ»ùÓÚÃû³ÆΪ”layouts.app”µÄ½á¹¹£¬²¢°üÀ¨Ò»¸öÎÊÌâΪ”All Posts”µÄHTMLÔªËØ¡£
Îå¡¢×ܽá
ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÏÈÈÝÁËÔõÑù×°ÖÃLaravel¿ò¼ÜºÍÌìÉú³õʼӦÓóÌÐò£¬ÒÔ¼°ÔõÑùʹÓÃLaravelµÄÊý¾Ý¿âǨáãºÍ¿ØÖÆÆ÷/ÊÓͼÎļþ½¨ÉèWebÓ¦ÓóÌÐòµÄ»ù´¡½á¹¹¡£
ʹÓÃLaravelÄܹ»¸ü¿ì¡¢¸ü±ãµ±µÄ½¨ÉèWebÓ¦ÓóÌÐò£¬²¢ÇÒÌṩÁËÖÚ¶àÏÖ´ú¿ª·¢¹¤¾ßºÍÊÖÒÕ£¬È繤¾ß¹ØϵӳÉ䣨ORM£©¡¢Ä£°åÒýÇæ¡¢RESTful APIµÈ¡£LaravelÒѳÉΪÆÕ±éʹÓõÄWebÓ¦ÓóÌÐò¿ò¼Ü£¬ÎÞÂÛÄúÊÇÐÂÊÖÕÕ¾ÉÂÄÀú¸»ºñµÄ¿ª·¢Ö°Ô±£¬Ëü¶¼ÊÇÒ»¸öÖµµÃѧϰºÍʹÓõĿò¼Ü¡£
ÒÔÉϾÍÊÇlaravel¿ò¼ÜÔõôÌìÉúµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡