问题
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