沃梦达 / 编程问答 / php问题 / 正文

Laravel 表单 html 与 PUT 方法的 PUT 路由

Laravel form html with PUT method for PUT routes(Laravel 表单 html 与 PUT 方法的 PUT 路由)

本文介绍了Laravel 表单 html 与 PUT 方法的 PUT 路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的路线中有这个:

+--------+---------------------------+--------------+--------------------------- ---------+-----------------+--------------+|域 |URI |姓名 |行动 |过滤器之前 |过滤后 |+--------+---------------------------+--------------+--------------------------- ---------+---------------+--------------+||得到|头/||后控制器|授权 ||||GET|HEAD 登录 ||homecontroller@dologin |||||POST 登录 ||homecontroller@dologin |||||GET|HEAD 注销 ||homecontroller@dologout |||||获取|头贴|post.index |postcontroller@index |||||GET|HEAD 发布/创建 |post.create |postcontroller@create |||||发布帖子 |邮局 |postcontroller@store |||||GET|HEAD post/{post} |post.show |postcontroller@show |||||GET|HEAD post/{post}/edit |后期编辑|postcontroller@edit |||||PUT post/{post} |更新后|postcontroller@update |||||补丁帖子/{post} ||postcontroller@update |||||删除帖子/{帖子} |销毁后|后控制器@destroy

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

<form class="col-md-12" action="<?php echo URL::to('/');?>/post/<?=$post->postID?>"方法=放置"><div class="form-group"><textarea type="text" class="form-control input-lg" placeholder="Text Here" name="post"><?=$post->post?></textarea>

<div class="form-group"><button class="btn btn-primary btn-lg btn-block" type="submit" value="Edit">Edit</button>

</表单>

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

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

{{form:...等

但是,我希望表单仍然可以通过 CSS 样式来完成.各位大侠有解决办法吗?谢谢

解决方案

您可以添加 css 类,以及刀片模板所需的任何类型的属性,试试这个:

{{ Form::open(array('url' => '/', 'method' => 'PUT', 'class'=>;'col-md-12'))}}.... 这里的代码{{ 表单::关闭() }}

如果你不想走刀片的方式,你可以添加一个隐藏的输入.无论如何,这就是 Laravel 的形式:

<块引用>

注意:由于 HTML 表单只支持 POST 和 GET、PUT 和 DELETE方法将通过自动添加 _method 隐藏字段来欺骗到你的表格.(Laravel 文档)

<!-- 呈现的刀片 HTML 表单使用此隐藏.别忘了把表单方法放到 POST --><输入名称=_方法";类型=隐藏"值=PUT"><div class="form-group"><textarea type="text"类=表单控制输入-lg"占位符=此处为文本"name="post"><?=$post->post?></textarea>

<div class="form-group"><button class="btn btn-primary btn-lg btn-block";类型=提交"值=编辑">编辑"

</表单>

I Have this in my 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 

Now, i want to make a form html that will use PUT method. Here it is my codes:

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

But i doesn't work to submit the form into post.edit.

I Have googled and i got solution that i must use

{{form:...etc

But, i want the form still can done by CSS styling. Is there any solution guys? Thank You

解决方案

You CAN add css clases, and any type of attributes you need to blade template, try this:

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

If you dont want to go the blade way you can add a hidden input. This is the form Laravel does, any way:

Note: Since HTML forms only support POST and GET, PUT and DELETE methods will be spoofed by automatically adding a _method hidden field to your form. (Laravel docs)

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

这篇关于Laravel 表单 html 与 PUT 方法的 PUT 路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Laravel 表单 html 与 PUT 方法的 PUT 路由

基础教程推荐