简体   繁体   English

Laravel - 此路由不支持 POST 方法。 支持的方法:GET、HEAD

[英]Laravel - The POST method is not supported for this route. Supported methods: GET, HEAD

I am trying to add an event on a calendar I have created, however I am getting the following error我正在尝试在我创建的日历上添加一个事件,但是我收到以下错误

The POST method is not supported for this route.此路由不支持 POST 方法。 Supported methods: GET, HEAD支持的方法:GET、HEAD

I have used the methods @csrf and {{ method_field('PUT') }} to no avail.我已经使用了@csrf 和 {{ method_field('PUT') }} 方法,但无济于事。 I have also cleared route cache which did not help.我还清除了没有帮助的路由缓存。 Any help is much appreciated.任何帮助深表感谢。

Routes:路线:

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Route::namespace('Admin')->prefix('admin')->name('admin.')->group(function(){
    Route::middleware('can:manage-users')->group(function(){
        Route::resource('/users', 'UsersController', ['except' => ['show']]);
        Route::resource('/courses', 'CoursesController', ['except' => ['show']]);
    });
    Route::middleware('can:manage-calendar')->group(function(){
        Route::get('events', 'EventsController@index')->name('events.index');
        Route::post('/addEvents', 'EventsController@addEvent')->name('events.add');
    });
})

index.blade.php index.blade.php

@extends('layouts.app')
@section ('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-14">
                <div class="card">
                        <div class="card-header">Calendar</div>
                        <div class="card-body">
                        {!! Form::open(array('route' => 'admin.events.index', 'method' => 'POST', 'files' => 'true'))!!}
                            {{-- {{method_field('PUT') }}  
                             @csrf  --}}
                        <div class="row">
                            <div class="col-xs-12 col-sm-12 col-md-12"></div>
                            <div class="col-xs-4 col-sm-4 col-md-4">
                            <div class="form-group">
                            {!! Form::label('event_name', 'Event Name:') !!}
                                <div class="">
                                    {!! Form::text('event_name', null, ['class' => 'form-control']) !!}
                                    {!! $errors->first('event_name', '<p class="alert alert-danger">:message</p>') !!}
                                </div>

@Collin, I have added the image below in relation to your question @Collin,我在下面添加了与您的问题相关的图片

在此处输入图片说明

The error actually explains the problem.该错误实际上解释了问题。 The method POST is not supported for the route you're using.您使用的路由不支持 POST 方法。 You are trying to post to the route: admin.events.index but you actually want to post to the route events.add.您正在尝试发布到路由:admin.events.index 但您实际上想要发布到路由 events.add。

Route::post('/addEvents', 'EventsController@addEvent')->name('events.add');

  {!! Form::open(array('route' => 'admin.events.add', 'method' => 'POST', 'files' => 'true'))!!}

                            {{-- @csrf  --}}

Adding to this awnser is a possible solution for the validator exception the OP has mentioned in the comments.添加到此 awnser 是 OP 在评论中提到的验证器异常的可能解决方案。

The validator not found error can possibly come from the following:未找到验证器错误可能来自以下原因:

When adding the the following code:添加以下代码时:

    public function addEvent(Request $request) 
{ 
$validator = Validator::make($request->all(), 
[ 'event_name' => 'required', 
'start_date' => 'required', 
'end_date' => 'required' ]); 

if ($validator->fails()) 
{ \Session::flash('warning', 'Please enter the valid details'); return Redirect::to('admin.events.index')->withInput()->withErrors($validator);

Try adding:尝试添加:

use Illuminate\Support\Facades\Validator;

Just check your form action url route.只需检查您的表单操作 url 路由。 You have to pass 'route('admin.events.add)' rather than 'route('admin.events.index')' and also dont use 'PUT' it will accept 'POST' as well.您必须传递 'route('admin.events.add)' 而不是 'route('admin.events.index')' 并且不要使用 'PUT' 它也会接受 'POST'。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 此路由不支持 POST 方法。 支持的方法:GET、HEAD (LARAVEL) - POST method is not supported for this route. Supported methods: GET, HEAD (LARAVEL) 此路由不支持 POST 方法。 支持的方法:GET、HEAD In Laravel - The POST method is not supported for this route. Supported methods: GET, HEAD In Laravel 此路由不支持 POST 方法。 支持的方法:GET、HEAD、PUT - The POST method is not supported for this route. Supported methods: GET, HEAD, PUT LARAVEL 9 此路由不支持 PUT 方法。 支持的方法:GET、HEAD - LARAVEL 9 The PUT method is not supported for this route. Supported methods: GET, HEAD 错误:此路由不支持 POST 方法。 支持的方法:GET、HEAD - Error:The POST method is not supported for this route. Supported methods: GET, HEAD 此路由不支持 POST 方法。 支持的方法:GET、HEAD - POST method is not supported for this route. Supported methods: GET, HEAD 此路由不支持 POST 方法。 支持的方法:GET、HEAD - The POST method is not supported for this route. Supported methods: GET, HEAD 此路由不支持 POST 方法。 支持的方法: GET、HEAD 在 laravel 5.8 - The POST Method Is Not Supported For This Route. Supported methods: GET, HEAD In laravel 5.8 Laravel 错误:此路由不支持 PUT 方法。 支持的方法:GET、HEAD、POST - Laravel error: The PUT method is not supported for this route. Supported methods: GET, HEAD, POST 此路由不支持 PUT 方法。 支持的方法:GET、HEAD、POST Laravel 6 - PUT method not supported for this route. Supported methods: GET, HEAD, POST Laravel 6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM