关键词搜索

源码搜索 ×
×

Laravel笔记-搭建Restful风格的后端

发布2023-02-13浏览1589次

详情内容

首先创建Controller和resource

  1. php artisan make:controller ProductsController -r
  2. php artisan make:resource ProductResource

将ProductsController.php改为:

  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Resources\ProductResource;
  4. use App\Models\Products;
  5. use Illuminate\Http\Request;
  6. class ProductsController extends Controller
  7. {
  8. /**
  9. * Display a listing of the resource.
  10. *
  11. * @return \Illuminate\Http\Response
  12. */
  13. public function index()
  14. {
  15. return Products::all();
  16. }
  17. /**
  18. * Show the form for creating a new resource.
  19. *
  20. * @return \Illuminate\Http\Response
  21. */
  22. public function create()
  23. {
  24. //
  25. }
  26. /**
  27. * Store a newly created resource in storage.
  28. *
  29. * @param \Illuminate\Http\Request $request
  30. * @return \Illuminate\Http\Response
  31. */
  32. public function store(Request $request)
  33. {
  34. $productName = $request->input('name');
  35. $productPrice = $request->input('price');
  36. $productDescription = $request->input('description');
  37. $product = Products::create([
  38. 'name' => $productName,
  39. 'price' => $productPrice,
  40. 'description' => $productDescription,
  41. ]);
  42. return response()->json([
  43. 'data' => new ProductResource($product)
  44. ], 201);
  45. }
  46. /**
  47. * Display the specified resource.
  48. *
  49. * @param int $id
  50. * @return \Illuminate\Http\Response
  51. */
  52. public function show(Products $product)
  53. {
  54. return new ProductResource($product);
  55. }
  56. /**
  57. * Show the form for editing the specified resource.
  58. *
  59. * @param int $id
  60. * @return \Illuminate\Http\Response
  61. */
  62. public function edit($id)
  63. {
  64. //
  65. }
  66. /**
  67. * Update the specified resource in storage.
  68. *
  69. * @param \Illuminate\Http\Request $request
  70. * @param int $id
  71. * @return \Illuminate\Http\Response
  72. */
  73. public function update(Request $request, Products $product)
  74. {
  75. $productName = $request->input('name');
  76. $productPrice = $request->input('price');
  77. $productDescription = $request->input('description');
  78. $product->update([
  79. 'name' => $productName,
  80. 'price' => $productPrice,
  81. 'description' => $productDescription
  82. ]);
  83. return response()->json([
  84. 'data' => new ProductResource($product)
  85. ], 200);
  86. }
  87. /**
  88. * Remove the specified resource from storage.
  89. *
  90. * @param int $id
  91. * @return \Illuminate\Http\Response
  92. */
  93. public function destroy(Products &$product)
  94. {
  95. $product->delete();
  96. return response()->json(null, 204);
  97. }
  98. }

PrductResource.php

  1. <?php
  2. namespace App\Http\Resources;
  3. use Illuminate\Http\Resources\Json\JsonResource;
  4. class ProductResource extends JsonResource
  5. {
  6. /**
  7. * Transform the resource into an array.
  8. *
  9. * @param \Illuminate\Http\Request $request
  10. * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
  11. */
  12. public function toArray($request)
  13. {
  14. return [
  15. 'id' => $this->id,
  16. 'productName' => $this->name,
  17. 'discountedPrice' => "$" . ($this->price * 0.8),
  18. 'discount' => "$" . ($this->price * 0.2),
  19. 'productDescription' => $this->description,
  20. ];
  21. }
  22. }

这里需要在routes/api.php中添加代码,不用在web.php中添加

  1. <?php
  2. use App\Http\Controllers\ProductsController;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\Route;
  5. /*
  6. |--------------------------------------------------------------------------
  7. | API Routes
  8. |--------------------------------------------------------------------------
  9. |
  10. | Here is where you can register API routes for your application. These
  11. | routes are loaded by the RouteServiceProvider within a group which
  12. | is assigned the "api" middleware group. Enjoy building your API!
  13. |
  14. */
  15. Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
  16. return $request->user();
  17. });
  18. Route::get('products', [ProductsController::class, 'index'])->name('products.index');
  19. Route::get('products/{product}', [ProductsController::class, 'show'])->name('products.show');
  20. Route::post('products', [ProductsController::class, 'store'])->name('products.store');
  21. Route::put('products/{product}', [ProductsController::class, 'update'])->name('products.update');
  22. Route::delete('products/{product}', [ProductsController::class, 'destroy'])->name('products.destroy');

运行截图如下:

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载