Laravel 9 错误路线 |不支持GET方法

发布于 2025-01-10 03:52:36 字数 2141 浏览 0 评论 0原文

我在通过 API 获取某些客户(经过身份验证的用户)的列表时遇到问题。当我在 Postman 中使用此路线时,收到以下错误。

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException: 该路由不支持 GET 方法。支持的方法: 邮政。在文件中 D:\api\vendor\laravel\framework\src\Illuminate\Routing\AbstractRouteCollection.php 第118行

api.php

Route::post('/register', [UserAuthController::class, 'register']);

Route::post('/login', [UserAuthController::class, 'login'])
    ->name('login');

Route::apiResource('/customer', CustomerController::class)
    ->middleware('auth:api');

控制器

class CustomerController extends Controller
{
    public function index()
    {
        $customers = Customer::all();

        return response([ 'customers' =>
            CustomerResource::collection($customers),
            'message' => 'Successful'], 200);
    }

    public function store(Request $request)
    {
        $data = $request->all();

        $validator = Validator::make($data, [
            'first_name'=>'required|max:120',
            'email'=>'required|email|unique:users',
            'password'=>'required|min:6'
        ]);

        if($validator->fails()){
            return response(['error' => $validator->errors(), 
                'Validation Error']);
        }

        $customer = Customer::create($data);

        return response([ 'customer' => new CustomerResource($customer),
            'message' => 'Success'], 200);
    }

    public function show(Customer $customer)
    {
        return response([ 'customer' => new CustomerResource($customer),
            'message' => 'Success'], 200);
    }
    
    public function update(Request $request, Customer $customer)
    {
        $customer->update($request->all());

        return response([ 'employee' => new CustomerResource($customer), 
            'message' => 'Success'], 200);
    }
    
    public function destroy(Customer $customer)
    {
        $customer->delete();

        return response(['message' => 'Customer deleted']);
    }
}

I am having a problem fetching a list of certain customers (authenticated users) via API. When I use this route in Postman I receive the following error.

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException:
The GET method is not supported for this route. Supported methods:
POST. in file
D:\api\vendor\laravel\framework\src\Illuminate\Routing\AbstractRouteCollection.php
on line 118

api.php

Route::post('/register', [UserAuthController::class, 'register']);

Route::post('/login', [UserAuthController::class, 'login'])
    ->name('login');

Route::apiResource('/customer', CustomerController::class)
    ->middleware('auth:api');

Controller

class CustomerController extends Controller
{
    public function index()
    {
        $customers = Customer::all();

        return response([ 'customers' =>
            CustomerResource::collection($customers),
            'message' => 'Successful'], 200);
    }

    public function store(Request $request)
    {
        $data = $request->all();

        $validator = Validator::make($data, [
            'first_name'=>'required|max:120',
            'email'=>'required|email|unique:users',
            'password'=>'required|min:6'
        ]);

        if($validator->fails()){
            return response(['error' => $validator->errors(), 
                'Validation Error']);
        }

        $customer = Customer::create($data);

        return response([ 'customer' => new CustomerResource($customer),
            'message' => 'Success'], 200);
    }

    public function show(Customer $customer)
    {
        return response([ 'customer' => new CustomerResource($customer),
            'message' => 'Success'], 200);
    }
    
    public function update(Request $request, Customer $customer)
    {
        $customer->update($request->all());

        return response([ 'employee' => new CustomerResource($customer), 
            'message' => 'Success'], 200);
    }
    
    public function destroy(Customer $customer)
    {
        $customer->delete();

        return response(['message' => 'Customer deleted']);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

大海や 2025-01-17 03:52:36

我通过在 Postman 的标头中添加 Accept|json/application 解决了这个问题。

I solved this problem by adding Accept|json/application in headers of Postman.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文