php Laravel 表单 html 与 PUT 方法的 PUT 路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28143674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel form html with PUT method for PUT routes
提问by Richard Henokh Kurniawan
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:
现在,我想制作一个使用 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>
But i doesn't work to submit the form into post.edit.
但我无法将表单提交到 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
但是,我希望表单仍然可以通过 CSS 样式来完成。各位大侠有解决办法吗?谢谢你
回答by Parvesh Kumar Tandon
If you are using HTML Form elementinstead Laravel Form Builder, you must place method_field
between your
form opening tag and closing end. By doing this you may explicitly define form methodtype.
如果您使用HTML Form 元素而不是Laravel Form Builder,则必须将其放置method_field
在表单开始标记和结束标记之间。通过这样做,您可以显式定义表单方法类型。
<form>
{{ method_field('PUT') }}
</form>
回答by Edgar Orozco
You CAN add css clases, and any type of attributes you need to blade template, try this:
您可以添加 css 类,以及刀片模板所需的任何类型的属性,试试这个:
{{ 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:
如果你不想走刀片的方式,你可以添加一个隐藏的输入。这是 Laravel 的形式,无论如何:
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)
注意:由于 HTML 表单仅支持 POST 和 GET,PUT 和 DELETE 方法将通过自动向表单添加 _method 隐藏字段来欺骗。(Laravel 文档)
<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>
回答by ZZA
Just use like this somewhere inside the form
就像这样在表单内的某个地方使用
@method('PUT')
回答by Radames E. Hernandez
Is very easy, you just need to use method_field('PUT')
like this:
很简单,你只需要method_field('PUT')
像这样使用:
HTML:
HTML:
<form action="{{ route('route_name') }}" method="post">
{{ method_field('PUT') }}
{{ csrf_field() }}
</form>
or
或者
<form action="{{ route('route_name') }}" method="post">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
Regards!
问候!
回答by Dark Cyber
in your view blade change to
在您看来刀片更改为
{{ 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() }}
actually you can use raw form like your question. but i dont recomended it. dan itulah salah satu alasan agan belajar framework, simple, dan cepat. so kenapa pake raw form kalo ada yang lebih mudah. hehe. proud to be indonesian.
实际上你可以像你的问题一样使用原始形式。但我不推荐它。dan itulah salah satu alasan agan belajar 框架,简单,dan cepat。所以kenapa pake生形式kalo ada yang lebih mudah。呵呵。为自己是印度尼西亚人而自豪。
reference: Laravel Blade Form