带有 POST 的 Laravel href
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37806171/
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 href with POST
提问by WellNo
I'm trying to pass some data to my controller with an action href
. I don't know why, but laravel passes the data with GETmethod, but instead of GETI need a POST. I don't really understand why laravel does that and coulnd't find an answer. I did that multiple times and my syntax seems to be correct. Can somebody have a look over it?
我正在尝试通过 action 将一些数据传递给我的控制器href
。我不知道为什么,但 Laravel 使用GET方法传递数据,但我需要一个POST而不是GET。我真的不明白为什么 laravel 这样做并且找不到答案。我做了多次,我的语法似乎是正确的。有人可以看看吗?
Blade:
刀刃:
<td>
@foreach($products as $product)
<a href="{{ action('ProductsController@delete', $product->id ) }}">
<span class="glyphicon glyphicon-trash"></span></a>
{{ $product->name }},
@endforeach
</td>
My Route:
我的路线:
Route::post('delete', ['as' => 'delete', 'uses' => 'ProductController@delete']);
In my Controller is just a:
在我的控制器中只是一个:
public function delete()
{
return 'hello'; // just testing if it works
}
Error:
错误:
MethodNotAllowedHttpException in RouteCollection.php line 219....
I know it's a get method, cause if I'm trying to pass the data to my controller, my URL looks like this:
我知道这是一个 get 方法,因为如果我试图将数据传递给我的控制器,我的 URL 如下所示:
blabla.../products/delete?10
Is anything wrong with my syntax? I can't really see why it uses the get method.
I also tried a: data-method="post"
insite of my <a>
tag but this haven't worked either.
我的语法有什么问题吗?我真的不明白为什么它使用 get 方法。我也试过 a: data-method="post"
insite of my <a>
tag 但这也没有用。
Thanks for taking time.
感谢您抽出时间。
回答by thefallen
When you make a link with an anchor like <a href=example.com>
your method will always be GET. This is like opening a URL in your browser, you make a GET request.
当您使用锚点创建链接时,<a href=example.com>
您的方法将始终为 GET。这就像在浏览器中打开一个 URL,您发出一个 GET 请求。
You should use a form to make that POST request to the delete method of the controller. Assuming you have the Illuminate HTML package for HTML and forms, you could do this:
您应该使用表单向控制器的删除方法发出 POST 请求。假设你有 HTML 和表单的 Illuminate HTML 包,你可以这样做:
{!! Form::open(['method' => 'DELETE', 'route' => $route]) !!}
{!! Form::submit('delete', ['onclick' => 'return confirm("Are you sure?");']) !!}
{!! Form::close() !!}
EDIT:With a button tag:
编辑:带有按钮标签:
{!! Form::open(['method' => 'DELETE', 'route' => $route]) !!}
<button type="submit"><i class="glyphicon glyphicon-remove"></i>Delete</button>
{!! Form::close() !!}
回答by Justin Origin Broadband
Here's your problem:
这是你的问题:
<a href="{{ action('ProductsController@delete', $product->id ) }}">
Anchor tags are always submitted over GET. It's a HTTP built in and is not Laravel specific.
锚标签总是通过 GET 提交。它是一个内置的 HTTP,不是 Laravel 特有的。
POST is used when a form is submitted that specifies the POST HTTP Verb or the HTTP method is invoked by an AJAX request that specifies POST as the HTTP Verb.
当提交指定 POST HTTP Verb 的表单或指定 POST 作为 HTTP Verb 的 AJAX 请求调用 HTTP 方法时,将使用 POST。
Instead consider a submit
type button in a form that submits what you need.
相反,请考虑submit
提交您需要的表单中的类型按钮。
<td>
@foreach($products as $product)
<form method="POST" action="{{ route('delete') }}">
<input type="hidden" name="product_id" value="{{ $product->id }}">
{!! csrf_field() !!}
<button type="submit" class="btn">
<span class="glyphicon glyphicon-trash"></span>
</button>
</form>
{{ $product->name }},
@endforeach
</td>
And then in your controller:
然后在您的控制器中:
public function delete()
{
// 'Die Dump' all of the input from the form / request
dd( request()->input()->all() );
// 'Die Dump' the specific input from the form
dd( request()->input('product_id') );
}
You will begin to see how GET and POST requests differ in sending of key/value pairs.
您将开始看到 GET 和 POST 请求在发送键/值对方面有何不同。
For more information:
想要查询更多的信息:
回答by Kaushik shrimali
Best for laravel 7. First define the form with given form id.
最适合 Laravel 7。首先定义具有给定表单 id 的表单。
@auth
<form id="logout-form" action="{{ route('logout') }}" method="POST"
style="display: none;">
@csrf
</form>
@endauth
Then you can use the anchor tag for logout operation.In background,javascript working.Wherever the logout was fire then action to given form method of post method and logout route was working.
然后你可以使用锚标记进行注销操作。在后台,javascript工作。无论注销在哪里,然后对给定的post方法和注销路由的表单方法进行操作。
<a class="nav-link dropdown-toggle text-muted waves-effect waves-dark"
href="{{ route('logout') }}" onclick="event.preventDefault();document.getElementById('logout-form').submit();"
id="2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="mdi mdi-logout"></i>
</a>
Most benefit is the form was not loaded unnecesory.If the user was logged in so far its load otherwise not
最大的好处是没有不必要地加载表单。如果用户到目前为止登录,则加载否则不会