该路由不支持GET方法。支持的方法:PUT。但我正在使用PUT

时间:2019-10-11 03:49:49

标签: php laravel

  

我正在尝试更新我的帖子表,但是它一直给我一个方法错误,说我应该使用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>

4 个答案:

答案 0 :(得分:0)

您的表单如下。

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

答案 1 :(得分:0)

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

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

收件人

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

您忘记添加/

答案 2 :(得分:0)

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

best

application/x-www-form-urlencoded Put操作不使用post的典型表单数据。

答案 3 :(得分:0)

一旦您的.htaccess文件包含此行,请检查

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