Laravel RESTful API ??????? ???????? ????????????- ???????? API Explained
Laravel RESTful API ????? ???????? ????????????—???????? ??????????, ????, ?????????, ??????, API ?????????? ??????????? ??? ??????? ????? ????????? ??????? Laravel ????? ???????? API ?????? ???? ???? ??? ??? ???????? ?????????
?????? ???????!
???? ???? ???? ???? ???? ???????? ?????? ???? ?????, ???? Laravel REST API ????? ?????? ???? ???????? ???? ????, ??? ???? ????? ???????????? ??????? ???? ??????? ??? ????? ???? ????? ?????, ??? ???? ??? ????????? ???? ???? ???? ???? ??? ??? ?? ?????????
Laravel API ?? ??? ??? ??????? ?????????
Laravel ???? PHP ????? ???????????? ????????????? ?? API ????? ?????? ????? ??????? ?? ????????????????? ??? ????? ???? ??????? ???? ??????? ???? ????, ???? ???? ????? ???????????? ??? ???? ?????? ???????????? ???? ?????? Laravel API ?????? ?? ????? ?????????????? ???????? ???? ??????? ???? ??????? ????? ???? ??? ?????????? ?????—?Netflix, Spotify ??????? ????? ????? ????? ?????????????????? ?? ?????? ??????? ????
Laravel API ?? ???? ?????? ???:
??? ???? ???? ??? ??????? ?????
??????? ??????????
Laravel API ??????? ???? ????? ??? ????? ??????? ?????????? ???? ???? ????? ????? ??????? ?????????? ?????? ?????? API ???????? ????????? ????? ????? ???????? ??? ??? ???? ??????? ?????????? ???? ???? ????:
php artisan make:controller API/ProductController --api
??? app/Http/Controllers/API ??????????? ProductController ???? ???? ?????????? ???? ????? --api ??????? ?????? ??? ???? API ??????? ?????????? ?????? ???? ????
?????? ?????????
??????? ?????????? ??????? ???? ??????? ??? ???? ???? ??? ????? ???????? ??????? Laravel ?? routes/api.php ???????? ????? ?????? ??? ???? ???:
Route::apiResource('products', ProductController::class);
??? ?????? ProductController ?? ??? ?????? ?????? ??? ????? ????:
GET /products (index)
POST /products (store)
GET /products/{product} (show)
PUT /products/{product} (update)
DELETE /products/{product} (destroy)
???? ? ?????????
???? ?????? ???? ???? ??? ????????? ???? ???? ???? ???? ????? ?????????? ??? ??? ??? ??? ????:
php artisan make:model Product -m
??? app/Models/Product.php ???? ???? ??? database/migrations ??????????? ???? ????????? ???? ???? ????? ?????? ??? ????????? ????? ?????? ??????????? ???????? ????? ????
API ?????????? ????????? ???
??? ?????? ProductController ? ??????????? ???????? ????? ??? ???? ??? API ?????????????? ????????? ??? ????? ???? ???? ?????? ?????? ??:
public function index()
{
return Product::all();
}
public function store(Request $request)
{
$product = Product::create($request->all());
return response()->json($product, 201);
}
public function show($id)
{
$product = Product::findOrFail($id);
return $product;
}
????? index ?????? ??? ????????? ??????? ????, store ?????? ???? ????????? ???? ???? ??? show ?????? ???? ???????? ???? ????????? ??????? ?????
API ???????
API ??????? ???? ???? Laravel ?? ?????????????? ???? symfony/http-kernel ??? symfony/css-selector ??????????? ??????? ??? ???? ????? ??? ??? ???? ???? ????? ??? ????? ?????:
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
use App\Models\Product;
class ProductTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function can_get_all_products()
{
$products = Product::factory(3)->create();
$this->getJson(route('products.index'))
->assertJsonFragment([
'name' => $products[0]->name
]);
}
}
????? ???? ???? ????? ?????? ???? ????? ???? ?? ?????? index ?????? ???????? ??? ???? ?????
????? ???? ?????????
???? ???? ????? ????????? ?????? ?? ???? Laravel API ?? ???? ?????? ??? ??????:
<?php
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use App\Models\Product;
use Illuminate\Http\Request;
class ProductController extends Controller
{
public function index()
{
$products = Product::all();
return response()->json($products);
}
public function store(Request $request)
{
$product = Product::create($request->all());
return response()->json($product, 201);
}
public function show($id)
{
$product = Product::findOrFail($id);
return $product;
}
}
?? ??????????? ProductController ? ????? ???? ???????? - index, store ??? show? index ?????? ??? ????????? ??????? ????, store ?????? ???? ????????? ???? ???? ??? show ?????? ???? ???????? ???? ????????? ??????? ?????
??? ??? ?? ??????????? ???? ???? Laravel API ?????? ?????? ??? ?? ????? ???????? ??? ???? ???? ???? ???, ??? ????? ?? ??? ??? ???? ???????? ?????? ????????????????? ????????????? ??? ???????????????? ??????????? ????????? ?????? ??? ???? ????????? ??, ???????? ????? ?? ???????? ?????? ???? ???? ?????? ???????!
web design and development
10 个月This is so amazing ??