【问题标题】:Sending POST request with GET parameter in Laravel在 Laravel 中使用 GET 参数发送 POST 请求
【发布时间】:2018-04-23 23:48:55
【问题描述】:

例如,我的 web.php 中有这条路线:-

Route::get('products/{product}/owners', 'ProductController@showOwners');

当我尝试向产品添加新的“所有者”时,我必须在父 URL 中进行,如下所示:-

Route::post('products/storeOwner', 'ProductController@storeOwner');

然后我在表单的隐藏字段中传递产品 ID,因为发布请求不接受 URL 参数。那么有没有像下面那样做呢?

Route::post('products/{product}/storeOwner', 'ProductController@storeOwner');

所以 POST 请求将在特定的“产品”URL 中发送?
更新

/* ProductController Class */
public function storeOwner (AddProductOwner $request)
    {

        $product= Product::find($request->product);
        $user = Auth::user();

        if ( $user->ownerOf($product)) {
            // Check if the current user is already one of the owners).
            // If the current user is the owner then return to the product
            // This line is not executed because in (products/show.blade.php) we have set a condition.

            return redirect('products/' . $request->product);
        }

        $join = new Join;
        $join->role = $request->join_role;
        $join->product()->associate($request->product);
        $join->user()->associate(Auth::user());

        $join->message = $request->message;


        $join->save();

        // TODO: we have to make this with ajax instead of normal form
        return redirect('products/'. $request->product);
}



我希望我的问题足够清楚..

【问题讨论】:

  • 当然。如果路由和 URL 定义正确,这将起作用。
  • POST 请求不接受 URL 中的参数
  • 我一直在使用它,就像下面的两个答案一样。 Laravel 就是这样聪明的。从技术上讲,它不是 POST,而是 URL 解析。

标签: php laravel post get routes


【解决方案1】:

是的,你可以按照你在上一条路线中提到的那样做

Route::post('products/{product}/storeOwner', 'ProductController@storeOwner');

然后在你的函数参数中获取产品 ID

public function storeOwner (AddProductOwner $request, $productId)
{
    dd($productId);    // TRY THIS OUT. CHECK THE 2nd ARGUMENT I SET.
    $product= Product::find($productId);    // PASS THE VERIABLE HERE.
    $user = Auth::user();

    if ( $user->ownerOf($product)) {
        // Check if the current user is already one of the owners).
        // If the current user is the owner then return to the product
        // This line is not executed because in (products/show.blade.php) we have set a condition.

        return redirect('products/' . $request->product);
    }

    $join = new Join;
    $join->role = $request->join_role;
    $join->product()->associate($request->product);
    $join->user()->associate(Auth::user());

    $join->message = $request->message;


    $join->save();

    // TODO: we have to make this with ajax instead of normal form
    return redirect('products/'. $request->product);

}

【讨论】:

  • 我试过这个方法,POST请求不允许发送URL参数
  • 你能在你的问题中发布函数storeOwner吗?
  • 更新问题,谢谢
  • @HamedAdil,相应地更新了我的答案。试一试。
  • 完美!,在深入研究代码后,它就像魅力一样,非常感谢
【解决方案2】:

您可以将 URL 参数发送到 POST 请求。只需确保在您的表单中发送通配符即可。

 <form action="/products/{{ $productid }}/storeOwner" method="POST">

在你的路线中

Route::post('products/{productid}/storeOwner', 'ProductController@storeOwner');

在你的控制器中,使用它

public function storeOwner($productid)
{
    dd($productid);
}

【讨论】:

    猜你喜欢
    • 2018-09-04
    • 2018-12-14
    • 2015-07-17
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多