Laravel 表单 html 与 PUT 方法用于 PUT 路由


Laravel form html with PUT method for PUT routes

我的路线中有这个:

+--------+---------------------------+--------------+---------------------------                                                                                                                ---------+----------------+---------------+
| Domain | URI                       | Name         | Action                                                                                                                                             | Before Filters | After Filters |
+--------+---------------------------+--------------+---------------------------                                                                                                                ---------+----------------+---------------+
|        | GET|HEAD /                |              | postcontroller                                                                                                                                     | auth           |               |
|        | GET|HEAD login            |              | homecontroller@dologin                                                                                                                             |                |               |
|        | POST login                |              | homecontroller@dologin                                                                                                                             |                |               |
|        | GET|HEAD logout           |              | homecontroller@dologout                                                                                                                            |                |               |
|        | GET|HEAD post             | post.index   | postcontroller@index                                                                                                                               |                |               |
|        | GET|HEAD post/create      | post.create  | postcontroller@create                                                                                                                              |                |               |
|        | POST post                 | post.store   | postcontroller@store                                                                                                                               |                |               |
|        | GET|HEAD post/{post}      | post.show    | postcontroller@show                                                                                                                                |                |               |
|        | GET|HEAD post/{post}/edit | post.edit    | postcontroller@edit                                                                                                                                |                |               |
|        | PUT post/{post}           | post.update  | postcontroller@update                                                                                                                              |                |               |
|        | PATCH post/{post}         |              | postcontroller@update                                                                                                                              |                |               |
|        | DELETE post/{post}        | post.destroy | postcontroller@destroy 

现在,我想制作一个将使用PUT方法的表单html。这是我的代码:

<form class="col-md-12" action="<?php echo URL::to('/');?>/post/<?=$post->postID?>" method="put">
    <div class="form-group">
        <textarea type="text" class="form-control input-lg" placeholder="Text Here" name="post"><?=$post->post?></textarea>
    </div>
    <div class="form-group">
        <button class="btn btn-primary btn-lg btn-block" type="submit" value="Edit">Edit</button>
    </div>
</form>     

但我无法将表单提交到 post.edit 中。

我已经用谷歌搜索过,我得到了我必须使用的解决方案

{{form:...etc

但是,我希望表单仍然可以通过CSS样式来完成。有什么解决方案吗?谢谢

如果您使用的是HTML表单元素而不是Laravel表单生成器,则必须将method_field放在您的形成开始标记和结束结束。通过这样做,您可以显式定义表单方法类型。

<form>
{{ method_field('PUT') }}
</form>

适用于Laravel 5.1及以上

<form>
@method('PUT')
</form>
只需在

表单中的某个地方使用即可

@method('PUT')

您可以添加 css clas,以及您需要刀片模板的任何类型的属性,请尝试以下操作:

{{ Form::open(array('url' => '/', 'method' => 'PUT', 'class'=>'col-md-12')) }}
.... wathever code here
{{ Form::close() }}

如果不想采用刀片式服务器方式,可以添加隐藏的输入。这是Laravel所做的形式,无论如何:

注意:由于 HTML 表单仅支持 POST 和 GET,因此 PUT 和 DELETE方法将通过自动添加_method隐藏字段来欺骗到您的表单。(拉拉维尔文档)

<form class="col-md-12" action="<?php echo URL::to('/');?>/post/<?=$post->postID?>" method="POST">
    <!-- Rendered blade HTML form use this hidden. Dont forget to put the form method to POST -->
    <input name="_method" type="hidden" value="PUT">
    <div class="form-group">
        <textarea type="text" class="form-control input-lg" placeholder="Text Here" name="post"><?=$post->post?></textarea>
    </div>
    <div class="form-group">
        <button class="btn btn-primary btn-lg btn-block" type="submit" value="Edit">Edit</button>
    </div>
</form>
非常简单

,你只需要使用这样的method_field('PUT')

方法一

<form action="{{ route('route_name') }}" method="post">
    {{ method_field('PUT') }}
    {{ csrf_field() }}
</form>

方法2

<form action="{{ route('route_name') }}" method="post">
    <input type="hidden" name="_method" value="PUT">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
  • 更新:我添加了与最新Laravel版本一起使用的新方法

方法3

<form action="{{ route('route_name') }}" method="post">
    @method('PUT')
    @csrf
</form>

PUT 路由的 PUT 方法

问候!

Laravel 8 上,您可以执行以下操作:

<form action="{{ route('post.update', $post->id) }}" method="POST">
    @method('PUT')
    @csrf
</form>
使用路由

帮助程序方法,您可以开始使用路由名称,这比使用完整路由更舒适。这就是为什么我做route('post.update', ...).

您肯定需要要编辑的资源的 id,因此$post->id route(..., $post->id) .

@method('PUT')@csrf做的与上面的答案完全相同......它只是看起来更好。

<form action="{{url('/url_part_in_route').'/'.$parameter_of_update_function_of_resource_controller}}"  method="post">
@csrf
<input type="hidden" name="_method" value="PUT">    //   or @method('put')          
....    //  remained instructions                                                                              
<form>
<form method="POST">
    @csrf
    @method('PUT') // or <input type="hidden" name="_method" value="PUT"> 
</form>

视图中更改为

{{ Form::open(['action' => 'postcontroller@edit', 'method' => 'PUT', 'class' = 'your class here']) }}
<div>
{{ Form::textarea('textareanamehere', 'default value here', ['placeholder' => 'your place holder here', 'class' => 'your class here']) }}
</div>
<div>
{{ Form::submit('Update', ['class' => 'btn class here'])}}
</div>
{{ Form::close() }}

实际上,您可以像您的问题一样使用原始形式。 但我不推荐它。 Dan Itulah Salah satu alasan agan belajar framework, simple, dan cepat. 所以 kenapa pake raw form kalo ada yang lebih mudah。 呵呵。 自豪地成为印度尼西亚人。

参考:拉拉维尔刀片形式