【问题标题】:The GET method is not supported for this route. Supported methods: PUT. but i'm using PUT此路由不支持 GET 方法。支持的方法:PUT。但我正在使用 PUT
【发布时间】:2019-10-11 03:49:49
【问题描述】:

我正在尝试更新我的 post 表,但它一直给我一个方法错误,说我应该使用 PUT 而不是 GET

问题是我正在使用PUT。我尝试多次更改@crsf 并更改@method('PUT'),包括隐藏输入。当我调用route:list 时,它表明我正在使用PUT 方法,但它仍然给我同样的错误。我不知道它可能是什么,有人知道吗?

    <section id="index" class="container">
        <div class="col-md-12 d-flex justify-content-center mt-5">
            <article class="new-post col-md-6 py-2">

                <header class="col-12 row d-flex justify-content-center">
                    <div class="add-header p-1">
                        <h2 class="m-2">Editar Post</h2>
                    </div>
                </header>

                <form method="POST" class="mt-5" action= "/edit_post/{id}" enctype="multipart/form-data">
                    @method('PUT')
                    @csrf

                    <div class="add-photo form-group d-flex justify-content-center p-5">
                        <label for="file-input">
                            <img src="{{ url($post->imagem) }}" alt="imagem do post">
                        </label>
                        <input id="file-input" name="imagem" type="file">
                    </div>

    // rotinas pagina index
    Route::get('/index', 'PostsController@posts');

    // crud posts
    Route::get('/add_post', 'PostsController@adicionandoPost');
    Route::post('/add_post', 'PostsController@salvandoPost');
    Route::put('/edit_post/{id}', 'PostsController@alterarPost');
    Route::delete('/edit_post{id}', 'PostsController@deletarPost');

    //crud usuario
    Route::get('/profile/{id}', 'UserController@profile');
    Route::put('/profile/{id}', 'UserController@alterarUsuario');
    Route::delete('/index', 'UserController@deletarUsuario')->name('delete');

});

PostsController:

public function alterarPost($id, Request $request) {
    $post = Post::findOrFail($id);

表格:


                    <input type="hidden" name="_method" value="PUT">
                    <input type="hidden" name="_token" value="{{ csrf_token() }}">

                    <div class="add-photo form-group d-flex justify-content-center p-5">
                        <label for="file-input">
                            <img src="{{ url($post->imagem) }}" alt="imagem do post">
                        </label>
                        <input id="file-input" name="imagem" type="file">
                    </div>

                    <input type="hidden" name="user_id" value="{{$post->id}}">

                    <div class="add-input form-group mt-2">
                        <input class="col-12 p-1" name="descricao" type="text" placeholder="Descrição" value="{{ $post->descricao }}">
                    </div>

                    <div class="add-input form-group">
                        <input class="col-12 p-1" name="tags" type="text" placeholder="Tags" value="{{ $post->tags }}">
                    </div>

                    <div class="add_photo d-flex justify-content-end p-3">
                        <button class="btn btn-danger m-1" type="button">Cancelar</button>
                        <button class="btn btn-success m-1" type="submit">Publicar</button>
                    </div>
                </form>

【问题讨论】:

  • 你试过运行php artisan route:clear了吗?
  • 是的!我试过运行 route:clear 和 cache:clear,但它仍然给我同样的错误。

标签: php laravel


【解决方案1】:

您的表单看起来像。

 <form method="post" class="mt-5" action= "{{ route('editpost',['id'=>$post->id]) }}" enctype="multipart/form-data">
        {{ method_field('PUT') }}
        {{ csrf_field() }}
    </form>

你的 web.php

Route::put('/edit_post/{id}', 'PostsController@alterarPost')->name('editpost');

【讨论】:

  • 尝试更改它,但它仍然给我同样的错误。比如说,索引中是否可能发生错误?因为它只在我点击链接时显示,可能是从索引获取和url之间存在冲突?
  • edit_post 被称为 alterarPostPostsController 方法。在您的问题中,您正在显示alterarUsuario 代码。尝试更改 URL 并对其进行调试,我认为您在某处误解了。
  • 哦抱歉,我粘贴错了,但在代码中是正确的。
【解决方案2】:

查看您的Web.php 您的路线有误 更改此:

Route::delete('/edit_post{id}', 'PostsController@deletarPost');

Route::delete('/edit_post/{id}', 'PostsController@deletarPost');

你忘了加/

【讨论】:

  • 哦,谢谢!以后会给我带来一些麻烦,但错误仍然是一样的。
【解决方案3】:

在您的表单中将 enctype 更改为:

application/x-www-form-urlencoded

PutPatch 操作不使用典型的 post 表单数据。

【讨论】:

    【解决方案4】:

    检查你的 .htaccess 文件,一旦它应该有这一行

     Header set Access-Control-Allow-Methods "GET,POST,PUT,DELETE,OPTIONS"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-12
      • 2019-12-29
      • 2020-04-03
      • 2020-05-27
      • 1970-01-01
      • 1970-01-01
      • 2019-11-14
      • 2020-04-13
      相关资源
      最近更新 更多