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

Posted IT1995

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Laravel笔记-搭建Restful风格的后端相关的知识,希望对你有一定的参考价值。

首先创建Controller和resource

php artisan make:controller ProductsController -r
php artisan make:resource ProductResource

将ProductsController.php改为:

<?php

namespace App\\Http\\Controllers;

use App\\Http\\Resources\\ProductResource;
use App\\Models\\Products;
use Illuminate\\Http\\Request;

class ProductsController extends Controller

    /**
     * Display a listing of the resource.
     *
     * @return \\Illuminate\\Http\\Response
     */
    public function index()
    
        return Products::all();
    

    /**
     * Show the form for creating a new resource.
     *
     * @return \\Illuminate\\Http\\Response
     */
    public function create()
    
        //
    

    /**
     * Store a newly created resource in storage.
     *
     * @param  \\Illuminate\\Http\\Request  $request
     * @return \\Illuminate\\Http\\Response
     */
    public function store(Request $request)
    
        $productName = $request->input('name');
        $productPrice = $request->input('price');
        $productDescription = $request->input('description');

        $product = Products::create([
            'name' => $productName,
            'price' => $productPrice,
            'description' => $productDescription,
        ]);
        return response()->json([
            'data' => new ProductResource($product)
        ], 201);
    

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \\Illuminate\\Http\\Response
     */
    public function show(Products $product)
    
        return new ProductResource($product);
    

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \\Illuminate\\Http\\Response
     */
    public function edit($id)
    
        //
    

    /**
     * Update the specified resource in storage.
     *
     * @param  \\Illuminate\\Http\\Request  $request
     * @param  int  $id
     * @return \\Illuminate\\Http\\Response
     */
    public function update(Request $request, Products $product)
    
        $productName = $request->input('name');
        $productPrice = $request->input('price');
        $productDescription = $request->input('description');

        $product->update([
            'name' => $productName,
            'price' => $productPrice,
            'description' => $productDescription
        ]);

        return response()->json([
            'data' => new ProductResource($product)
        ], 200);
    

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \\Illuminate\\Http\\Response
     */
    public function destroy(Products &$product)
    
        $product->delete();
        return response()->json(null, 204);
    

PrductResource.php

<?php

namespace App\\Http\\Resources;

use Illuminate\\Http\\Resources\\Json\\JsonResource;

class ProductResource extends JsonResource

    /**
     * Transform the resource into an array.
     *
     * @param  \\Illuminate\\Http\\Request  $request
     * @return array|\\Illuminate\\Contracts\\Support\\Arrayable|\\JsonSerializable
     */
    public function toArray($request)
    
        return [
          'id' => $this->id,
          'productName' => $this->name,
          'discountedPrice' => "$" . ($this->price * 0.8),
          'discount' => "$" . ($this->price * 0.2),
          'productDescription' => $this->description,
        ];
    

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

<?php

use App\\Http\\Controllers\\ProductsController;
use Illuminate\\Http\\Request;
use Illuminate\\Support\\Facades\\Route;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::middleware('auth:sanctum')->get('/user', function (Request $request) 
    return $request->user();
);


Route::get('products', [ProductsController::class, 'index'])->name('products.index');
Route::get('products/product', [ProductsController::class, 'show'])->name('products.show');
Route::post('products', [ProductsController::class, 'store'])->name('products.store');
Route::put('products/product', [ProductsController::class, 'update'])->name('products.update');
Route::delete('products/product', [ProductsController::class, 'destroy'])->name('products.destroy');

运行截图如下:

以上是关于Laravel笔记-搭建Restful风格的后端的主要内容,如果未能解决你的问题,请参考以下文章

Laravel文档阅读笔记-How to Build a Rest API with Laravel: A Beginners Guide①

Laravel文档阅读笔记-How to Build a Rest API with Laravel: A Beginners Guide①

Django后端开发学习笔记使用动态修改fields的序列化器实现指定字段的查询以及RESTful风格的信息过滤

Django后端开发学习笔记使用动态修改fields的序列化器实现指定字段的查询以及RESTful风格的信息过滤

Django后端开发学习笔记使用动态修改fields的序列化器实现指定字段的查询以及RESTful风格的信息过滤

当 AngularJS 的后端只需要 RESTful 应用程序时使用 JAVA EE 7