本文介绍了该路由不支持GET方法.支持的方法:POST/PATCH/DELETE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经手动创建了一些用于自定义目的的路由.

I have created manually some routes for customizing purposes.

这是我的代码:

Route::post('/dashboard/show-all-notifications', [App\Http\Controllers\DashboardController::class, 'showAllNotifications']);

表格

{!! Form::open(['method'=>'POST','action'=>['App\Http\Controllers\DashboardController@showAllNotifications']]) !!}
   {!! Form::submit('Show all notifications', ['class'=>'btn btn-sm btn-primary btn-block']) !!}
{!! Form::close() !!}

DashboardController

public function showAllNotifications(Request $request)
{
    if($request->isMethod('post'))
    {
        $notifications = Auth::user()->notifications;
        return view('dashboard.showAllNotifications',compact('notifications'));
    }
    else
    {
        return abort(404);
    }
}

当我在浏览器中输入 URL(GET request)时,向我显示此错误,但它在 POST/PATCH/DELETE 表单请求上起作用.我需要类似的请求(如果请求是 GET ),则返回到 404找不到.

It's showing me this error when I enter URL(GET request) in the browser but it working on the POST / PATCH / DELETE Form request. I need something like if the request is GET, the return to 404 not found.

有人知道该错误的解决方案吗?

推荐答案

直接从浏览器url访问发布路由会导致此错误.您可以使用 Route :: any() Route :: match()来处理这种情况.然后您可以检查请求方法.如果您愿意,请执行某项操作,否则中止至404.

accessing a post route directly from browser url causes this error. you can use either Route::any() or Route::match() to handle this kind of situation. and then you can check for request method. if it's your desired one, do something, otherwise abort to 404.

public function showAllNotifications(Request $request)
{
    if ($request->isMethod('post')) {
        //do something
    } else {
        abort(404);
    }
}

并且为您提供信息,在创建控制器时添加-r或--resource无法解决您的问题.这些选项用于添加操作的方法.您自己的方法和路由与资源无关.

and for your information, adding -r or --resource while creating a controller won't solve your issue. those options are for adding methods for crud operations. your own methods and routes have nothing to do with resource.

这篇关于该路由不支持GET方法.支持的方法:POST/PATCH/DELETE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 20:23