今天預計把整個流程一次衝完,來建立一個API吧 GOGOGO!!!!
建立新專案composer create-project laravel/laravel 專案名稱 "5.6"
然後 cd 專案資料夾,測試一下是否可以使用artisanphp artisan list
如果測試OK的話,就要開始囉!
通過artisan指令來建構controllerphp artisan make:controller ProductsController --resource
這樣就得到了擁有基礎CRUD架構的controller(先建立好而已,繼續往下走)
從昨天的構想來修改route
Routes/web.php
/**
** RESTful Route補完計畫
**
** Route::get($uri, $callback);
** Route::post($uri, $callback);
** Route::put($uri, $callback);
** Route::delete($uri, $callback);
**
**/
Route::resource('products', 'ProductsController', ['except' => ['create', 'edit']]);
Route::get('products', 'ProductsController@index');
Route::get('products/{product}', 'ProductsController@show');
Route::post('products','ProductsController@store');
Route::put('products/{product}','ProductsController@update');
Route::delete('products/{product}', 'ProductsController@delete');
看了Route的設定就有些許味道囉~GO!!
備註
有查到可以可以使用Route::resource('products', 'ProductsController', ['except' => ['create', 'edit']]); 但目前還沒用到 先忽略
把ProductsController的create 方法和 edit 方法排除
這時候還需要去修改 config/database.php 、.env兩個檔案
將 這些修改
(我就照wagon的貼上囉! 自填的部分就自行選擇,但記得要去建立資料庫喔)
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '33060'),
'database' => env('DB_DATABASE', '自填'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'root'),
DB_HOST=localhost
DB_PORT=33060
DB_DATABASE=自填
DB_USERNAME=root
DB_PASSWORD=root
這時候就可以來創建model了~順便連Migration一起弄一弄啦!!!php artisan make:model Product -m
檔案位置
database/migrations/timestamp_create_products_table.php
原本的Migration是這樣
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProductsTable extends Migration
{
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('products');
}
}
修改一下Migration 的up()
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('title');
$table->text('description');
$table->integer('price');
$table->boolean('availability');
});
}
執行遷移php artisan migrate
model檔案的位置
app/Product.php
來model新增一下白名單protected $fillable = ['title', 'description', 'price', 'availability'];
灌點假資料php artisan make:seeder ProductsTableSeeder
database/seeds/ProductsTableSeeder.php
use App\Product;
public function run()
{
$faker = \Faker\Factory::create();
// Create 50 product records
for ($i = 0; $i < 50; $i++) {
Product::create([
'title' => $faker->title,
'description' => $faker->paragraph,
'price' => $faker->randomNumber(2),
'availability' => $faker->boolean(50)
]);
}
}
執行php artisan db:seed --class=ProductsTableSeeder
補controller
app/HTTP/Controllers/ProductsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Product;
class ProductsController extends Controller
{
public function index()
{
return Product::all();
}
public function show(Product $product)
{
return $product;
}
public function store(Request $request)
{
$product = Product::create($request->all());
return response()->json($product, 201);
}
public function update(Request $request, Product $product)
{
$product->update($request->all());
return response()->json($product, 200);
}
public function delete(Product $product)
{
$product->delete();
return response()->json(null, 204);
}
}
這時候可以輸入
http://localhost:8000/products
http://localhost:8000/products/1
來看看目前的成果喔!!!
在今天我們幾乎把之前的步驟一次跑完,也順便將之前零星的部分都一次串了起來,雖然少了view的部分,但由這次的練習可以稍微感受一下Laravel整體的流程與開發的感覺囉
備註:
1.
/vendor/autoload.php): failed to open stream: No such file or directory in XXXXXXX\artisan
請輸入composer update看看
如果有報錯的話,都可以找看看檔案的中類似use app/Product.php 該加的地方有沒有加到喔!!!