Laravel POST 方法返回状态:405 不允许在 POST 方法上使用方法

问题描述 投票:0回答:1

请查找以下信息:

NoteController.php

<?php

namespace App\Http\Controllers;

use App\Http\Requests\NoteRequest;
use App\Models\Note;
use Illuminate\Http\JsonResponse;

class NoteController extends Controller
{
    public function index():JsonResponse
    {
        $notes = Note::all();
        return response()->json($notes, 200);
    }

    public function store(NoteRequest $request):JsonResponse
    {
        $note = Note::create( $request->all() );
        return response()->json([
            'success' => true,
            'data' => $note
        ], 201);
    }

    public function show($id):JsonResponse
    {
        $note = Note::find($id);
        return response()->json($note, 200);
    }

    public function update(NoteRequest $request, $id):JsonResponse
    {
        $note = Note::find($id);
        $note->update($request->all());
        return response()->json([
            'success' => true,
            'data' => $note,
        ], 200);
    }

    public function destroy($id):JsonResponse
    {
        Note::find($id)->delete();
        return response()->json([
            'success' => true
        ], 200);
    }
}

NoteRequest.php

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class NoteRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'title', 'required|max:255|min:3',
            'content', 'nullable|max:255|min:10',
        ];
    }
}

Note.php(模型)

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Note extends Model
{
    use HasFactory;

    protected $guarded = [];
}

api.php

<?php

use App\Http\Controllers\NoteController;
use Illuminate\Support\Facades\Route;

Route::prefix('v1')->group(function () {
    Route::resource('/note', NoteController::class);
});

php artisan 路线:列表

GET|HEAD        / ......................................................................................................................  
  POST            _ignition/execute-solution ............... ignition.executeSolution › Spatie\LaravelIgnition › ExecuteSolutionController  
  GET|HEAD        _ignition/health-check ........................... ignition.healthCheck › Spatie\LaravelIgnition › HealthCheckController  
  POST            _ignition/update-config ........................ ignition.updateConfig › Spatie\LaravelIgnition › UpdateConfigController  
  GET|HEAD        api/v1/note .......................................................................... note.index › NoteController@index  
  POST            api/v1/note .......................................................................... note.store › NoteController@store  
  GET|HEAD        api/v1/note/create ................................................................. note.create › NoteController@create  
  GET|HEAD        api/v1/note/{note} ..................................................................... note.show › NoteController@show  
  PUT|PATCH       api/v1/note/{note} ................................................................. note.update › NoteController@update  
  DELETE          api/v1/note/{note} ............................................................... note.destroy › NoteController@destroy  
  GET|HEAD        api/v1/note/{note}/edit ................................................................ note.edit › NoteController@edit  
  GET|HEAD        sanctum/csrf-cookie .................................. sanctum.csrf-cookie › Laravel\Sanctum › CsrfCookieController@show

迅雷请求(同邮递员) Thunder Client

JSON 请求

{
  "title": "Hello World",
  "content": "Lorem ipsum."
}

尝试发出 JSON POST 请求并获取状态:405 方法不允许并且我正在使用 php artisan 服务,如果需要,我可以提供 GIT 项目。请告诉我。

php laravel laravel-10 thunderclient
1个回答
0
投票

您的验证规则看起来不正确。在您的

NoteRequest
类中,规则应该是一个关联数组,其中键是字段名称,值是验证规则。但是,在您的代码中,规则被定义为以逗号分隔的字符串列表。这可能会导致验证失败并返回 405 Method Not allowed 错误。

public function rules()
{
    return [
        'title' => 'required|max:255|min:3',
        'content' => 'nullable|max:255|min:10',
    ];
}
© www.soinside.com 2019 - 2024. All rights reserved.