The GET method is not supported for this route. Supported methods: POST / PATCH / DELETE

亡梦爱人 提交于 2021-01-29 11:29:56

问题


I have created manually some routes for customizing purposes.

Here is my code:

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

Form

{!! 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);
    }
}

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.

Does anyone know the solution to this error ?


回答1:


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);
    }
}

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.



来源:https://stackoverflow.com/questions/64421225/the-get-method-is-not-supported-for-this-route-supported-methods-post-patch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!