ÁÄÁÄlaravel5.5ʵÏÖÏîÄ¿µÄ°ì·¨
laravelÊÇÒ»¸ö¾«Á·ÓÅÑÅ¡¢¿ª·¢Ð§ÂʸߵÄphp¿ò¼Ü£¬Ö¼ÔÚ¹¹½¨webÓ¦ÓóÌÐòºÍapi¡£Ëü±»ÆÕ±éÓ¦ÓÃÓÚweb¿ª·¢ÖУ¬ÒÔÆ俪·¢ËÙÂʺÍÓÅÒìµÄ´úÂë½á¹¹¶øÖø³Æ¡£
ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«Ñ§Ï°ÔõÑùʹÓÃlaravel5.5¿ò¼ÜÀ´ÊµÏÖÒ»¸öÏîÄ¿¡£ÎÒÃǽ«Ê¹ÓÃMVC(Model-View-Controller)ģʽÀ´¹¹½¨×ðÁú¿Ê±Ó¦ÓóÌÐò£¬²¢Ê¹ÓÃComposerÀ´ÖÎÀíÒÀÀµÏî¡£
×¼±¸ÊÂÇé
ÔÚ×îÏÈ֮ǰ£¬ÄúÐèҪװÖÃPHP 7.0»ò¸ü¸ß°æ±¾ÒÔ¼°MySQLÊý¾Ý¿â¡£ÄúÒ²ÐèҪװÖÃComposer£¬ÕâÊÇÒ»¸öPHP°ü¹ÜÀíÆ÷£¬ÎÒÃǽ«Ê¹ÓÃËüÔÚ×ðÁú¿Ê±ÏîÄ¿ÖÐ×°ÖÃËùÐèµÄÒÀÀµÏî¡£
½¨ÉèLaravelÏîÄ¿
Ê×ÏÈ£¬·¿ªÖն˲¢×ªµ½ÄúÏëÒª½¨ÉèÏîÄ¿µÄĿ¼¡£È»ºóʹÓÃÒÔÏÂÏÂÁîÀ´½¨ÉèÒ»¸öеÄLaravelÏîÄ¿£º
composer create-project --prefer-dist laravel/laravel project_name
µÇ¼ºó¸´ÖÆ
Õ⽫ÏÂÔغÍ×°ÖÃlaravel£¬ËùÐèµÄÒÀÀµÏîÒÔ¼°Ò»Ð©Ä¬ÈÏÎļþºÍÎļþ¼Ð¡£Çë×¢ÖØ£¬project_nameÊÇÄúÏëÒª½¨ÉèµÄÏîÄ¿µÄÃû³Æ¡£
ÉèÖÃÊý¾Ý¿â
LaravelĬÈÏʹÓÃSQLiteÊý¾Ý¿â£¬¿ÉÊÇÎÒÃǽ«ÔÚ´Ë´¦Ê¹ÓÃMySQLÊý¾Ý¿â¡£Îª´Ë£¬Çë·¿ª.envÎļþ²¢¸ü¸ÄÒÔÏÂÐУº
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=your_database_name DB_USERNAME=your_database_username DB_PASSWORD=your_database_password
µÇ¼ºó¸´ÖÆ
Çë×¢ÖØ£¬¸ü¸Äyour_database_name£¬your_database_usernameºÍyour_database_passwordÒÔ·´Ó¦ÄúµÄÏÖʵÊý¾Ý¿âƾ֤¡£
½¨ÉèÊý¾Ý¿â±í
ÔÚÎÒÃǼÌÐø±àд´úÂë֮ǰ£¬ÈÃÎÒÃǽ¨ÉèÊý¾Ý¿â±í¸ñ¡£ÎÒÃǽ«Ê¹ÓÃlaravelµÄǨá㹦ЧÀ´ÊµÏÖÕâÒ»µã¡£Ê¹ÓÃÒÔÏÂÏÂÁÉèÒ»¸öǨá㣺
php artisan make:migration create_tasks_table --create=tasks
µÇ¼ºó¸´ÖÆ
Õ⽫½¨ÉèÒ»¸öÃûΪcreate_tasks_tableµÄǨá㣬Ëü½«ÔÚÊý¾Ý¿âÖн¨ÉèÒ»¸öÃûΪtasksµÄ±í¡£·¿ªÇ¨áãÎļþ²¢ÔÚupÒªÁìÖÐÌí¼ÓÒÔÏ´úÂëÀ´½ç˵±í½á¹¹£º
public function up() { Schema::create('tasks', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->text('description'); $table->timestamps(); }); }
µÇ¼ºó¸´ÖÆ
¸Ã±í¸ñ¾ßÓÐÒ»¸ö×Ô¶¯µÝÔöµÄIDÁУ¬Ãû³ÆºÍÐÎòÁÐÒÔʵʱ¼ä´ÁÁС£
ÏÖÔÚ£¬ÔËÐÐÒÔÏÂÏÂÁîÀ´½¨Éè¸Ã±í£º
php artisan migrate
µÇ¼ºó¸´ÖÆ
Õ⽫ÔËÐÐǨáã²¢ÔÚÊý¾Ý¿âÖн¨Éè±í¸ñ¡£
½¨ÉèʹÃüÄ£×Ó
½ÓÏÂÀ´£¬ÎÒÃǽ«½¨ÉèÒ»¸öʹÃüÄ£×Ó¡£ÔÚLaravelÖУ¬Ä£×ÓÊÇÓëÊý¾Ý¿â±í¹ØÁªµÄÀࡣʹÓÃÒÔÏÂÏÂÁÉèÒ»¸öÃûΪTaskµÄÄ£×Ó£º
php artisan make:model Task
µÇ¼ºó¸´ÖÆ
Õ⽫ÔÚappĿ¼Öн¨ÉèÒ»¸öTask.phpÎļþ£¬ÆäÖаüÀ¨Ò»¸ö»ù±¾µÄ¿Õȱģ×Ó¡£
·¿ª¸ÃÎļþ²¢Ìí¼ÓÒÔÏÂÐÐÀ´½ç˵Óëtasks±í¸ñµÄ¹ØÁª£º
protected $table = 'tasks';
µÇ¼ºó¸´ÖÆ
½¨Éè¿ØÖÆÆ÷ºÍÊÓͼ
ÏÂÒ»²½Êǽ¨Éè¿ØÖÆÆ÷ºÍÊÓͼ¡£¿ØÖÆÆ÷ÊǸÃÓ¦ÓóÌÐòµÄÖ÷ÒªÓªÒµÂß¼£¬²¢·ºÆðÊÓͼ»òJSONÏìÓ¦¡£ÔÚLaravelÖУ¬ÎÒÃÇʹÓÿØÖÆÆ÷À´´¦Öóͷ£Ó¦ÓóÌÐòÖеÄ·ÓÉ¡£
ʹÓÃÒÔÏÂÏÂÁÉèÒ»¸öÃûΪTaskControllerµÄ¿ØÖÆÆ÷£º
php artisan make:controller TaskController --resource
µÇ¼ºó¸´ÖÆ
´ËÏÂÁ½¨ÉèTaskController.phpÎļþ£¬²¢Æ¾Ö¤RESTful·ÓÉÔ¼¶¨½ç˵ÖÖÖÖÒªÁì¡£
ÏÖÔÚ£¬ÎÒÃÇÐèҪΪÿ¸ö·Óɽ¨ÉèÊÓͼ¡£Ê¹ÓÃÒÔÏÂÏÂÁÉèÒ»¸öÃûΪindex.blade.phpµÄÊÓͼÎļþ£º
touch resources/views/tasks/index.blade.php
µÇ¼ºó¸´ÖÆ
ÔÚ¸ÃÎļþÖУ¬½«ÒÔÏÂÄÚÈÝÌí¼Óµ½HTML£º
@extends('layouts.app') @section('content') <div> <div> <div> <div> <div>Tasks</div> <div> <table> <thead> <tr> <th>#</th> <th>Name</th> <th>Description</th> <th>Created At</th> <th>Updated At</th> </tr> </thead> <tbody> @foreach ($tasks as $task) <tr> <th>{{$task->id}}</th> <td>{{$task->name}}</td> <td>{{$task->description}}</td> <td>{{$task->created_at->toFormattedDateString()}}</td> <td>{{$task->updated_at->toFormattedDateString()}}</td> </tr> @endforeach </tbody> </table> </div> </div> </div> </div> </div> @endsection
µÇ¼ºó¸´ÖÆ
´ËÊÓͼ½«ÔÚ±í¸ñÖÐÏÔʾËùÓÐʹÃü¡£
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒª¸üпØÖÆÆ÷ÒÔ·ºÆð´ËÊÓͼ²¢ÌáȡʹÃüÊý¾Ý¡£·¿ªTaskController.phpÎļþ²¢½«ÒÔÏÂÄÚÈÝÌí¼Óµ½indexÒªÁìÖУº
public function index() { $tasks = Task::all(); return view('tasks.index', compact('tasks')); }
µÇ¼ºó¸´ÖÆ
Õ⽫ÌáÈ¡ËùÓÐʹÃü²¢×ª´ï¸øÊÓͼÒÔÏÔʾËüÃÇ¡£
·ÓÉ
ÏÖÔÚ£¬ÎÒÃÇÐèÒª½«×ðÁú¿Ê±Â·ÓÉÉèÖõ½×ðÁú¿Ê±Ó¦ÓóÌÐòÖС£·¿ªroutes/web.phpÎļþ²¢Ìí¼ÓÒÔÏ·ÓÉ£º
Route::get('/', 'TaskController@index');
µÇ¼ºó¸´ÖÆ
Õ⽫½«¸ù·ÓÉÓ³Éäµ½TaskControllerµÄindexÒªÁì¡£
ÔËÐÐÓ¦ÓóÌÐò
ÏÖÔÚ£¬ÎÒÃÇÍê³ÉÁË×ðÁú¿Ê±Ó¦ÓóÌÐòµÄ±àдºÍÉèÖã¬ÈÃÎÒÃÇÔËÐÐËü£¡Ê¹ÓÃÒÔÏÂÏÂÁîÆô¶¯Ó¦ÓóÌÐò£º
php artisan serve
µÇ¼ºó¸´ÖÆ
ÔÚä¯ÀÀÆ÷Öе¼º½µ½http://localhost:8000£¬ÄúÓ¦¸ÃÄܹ»¿´µ½ÄúµÄÓ¦ÓóÌÐòÖеÄËùÓÐʹÃü¡£
½áÂÛ
ÔÚ±¾ÎÄÖÐ,ÎÒÃÇÏàʶÁËÔõÑùʹÓÃlaravel5.5¿ò¼ÜÀ´¹¹½¨Ò»¸ö¼òÆÓµÄÓ¦ÓóÌÐò¡£ÎÒÃǺ¸ÇÁËÆðÔ´µÄlaravel¿ª·¢Á÷³Ì£¬°üÀ¨ÔõÑùʹÓÃComposerÉèÖÃÒÀÀµ£¬ÔõÑùÉèÖÃÊý¾Ý¿â£¬ÔõÑù½¨ÉèÄ£×ÓºÍǨá㣬ÒÔ¼°ÔõÑù½¨Éè¿ØÖÆÆ÷ºÍÊÓͼ£¬²¢½«ÆäÓ³É䵽·ÓÉ¡£ÕâÊÇÒ»¸öºÜÊǼòÆÓµÄʾÀý£¬¿ÉÊÇÄú¿ÉÒÔͨ¹ý̽Ë÷¸ü¶àÎĵµºÍ×ÊÔ´À´½«ÆäÀ©Õ¹³ÉÄú×Ô¼ºµÄÓ¦ÓóÌÐò¡£
ÒÔÉϾÍÊÇÁÄÁÄlaravel5.5ʵÏÖÏîÄ¿µÄ°ì·¨µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡