Laravel RESTful API ??????? ???????? ????????????- ???????? API Explained
Laravel API Tutorial (Bangla)

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 ?????? ?????? ??? ?? ????? ???????? ??? ???? ???? ???? ???, ??? ????? ?? ??? ??? ???? ???????? ?????? ????????????????? ????????????? ??? ???????????????? ??????????? ????????? ?????? ??? ???? ????????? ??, ???????? ????? ?? ???????? ?????? ???? ???? ?????? ???????!

??? ????? ???? ???? ???? ????? Or read my English article on It.

Sabbirr rahman polok

web design and development

10 个月

This is so amazing ??

回复

要查看或添加评论,请登录

Abu Sayed的更多文章