【问题标题】:How to redirect back to a POST route in Laravel如何重定向回 Laravel 中的 POST 路由
【发布时间】:2019-12-23 07:08:11
【问题描述】:

我有一个价格范围搜索滑块。这适用于查找范围之间的价格。它工作正常。如果我设置一个范围并按下搜索按钮,它会返回搜索结果。但我在这里面临一个问题。我在“价格范围搜索”中有一些产品。我可以从这里查看详细信息。但是当我尝试“添加到购物车”时遇到了问题。

在我的 add-to-cart 路线中,有一个名为 add_to_cart 的函数,它将产品添加到购物车并返回到它来自的上一页。

在这种情况下,我面临一个问题“MethodNotAllowedHTTPException”,因为它是一个 POST 路由。

注意:我的 add_to_cart 功能工作正常。但是遇到问题return redirect()->back();

我也试试return redirect()->back()->withInput();

它也给我同样的错误。

有什么想法可以解决吗?

提前非常感谢!

【问题讨论】:

  • 你能显示你的两个路由和控制器方法的代码吗?
  • 你的路由方式是什么?邮政?您能否发布您的路线和控制器的代码
  • price-range-search 路线:- Route::post('/price-range-search', 'WelcomeController@price_range_search'); 添加到购物车 路线:- Route::match(['get', 'post'], '/add-to-cart/{id}', 'CartController@add_to_cart');

标签: laravel redirect routes return


【解决方案1】:

这只是从价格范围搜索 POST 控制器到添加到购物车 GET 控制器 并且添加到购物车的 GET 控制器方法只是将 back() 返回到来自请求的页面。在这种情况下,问题就出现了。它无法将 back() 返回到后控制器。那么可能的解决方案是什么!

价格范围搜索路线:- Route::post('/price-range-search', 'WelcomeController@price_range_search');

加入购物车路线:- Route::match(['get', 'post'], '/add-to-cart/{id}', 'CartController@add_to_cart');

checkRoutesimageshere:

price_range_search 控制器功能:- `

public function price_range_search(Request $request)
{
    $data = array();
    $data['price_range'] = $request->input('price_range');
    // echo $data['price_range'];
    $min_max = explode(',', $data['price_range']);
    $min = $min_max[0];
    $max = $min_max[1];

    $search_result = DB::table('tbl_product')
            ->whereBetween('product_price', array($min, $max))
            ->orderBy('product_id', 'desc')
            ->get();

    // echo "<pre>";
    // print_r($search_result);
    // exit();

    $search_content = view('pages.search')
                    ->with('min', $min)
                    ->with('max', $max)
                    ->with('search_result', $search_result);

    return view('index')
                    ->with('content', $search_content);
}

img of price_range_search Controller Function

添加到购物车路由控制器功能:-

公共函数 add_to_cart($id, Request $request) {

    $product_info = DB::table('tbl_product')
            ->where('product_id', $id)
            ->first();

    $data['id'] = $product_info->product_id;
    $data['name'] = $product_info->product_name;
    $data['price'] = $product_info->product_price;

    $qty = $request->product_quantity;
    if ($qty > 1) {
        $data['qty'] = $qty;
    } else {
        $data['qty'] = 1;
    }

    $data['options'] = array('image' => $product_info->product_image);

    Cart::add($data);
    Session::put('message', ' Your Cart Item(s) are Updated! ');

    // return back()->send();
    return redirect()->back()->withInput(Input::all());
    // return Redirect::to('/show-cart');
}

` img of add-to-cart Controller Function:-

注意:这只是从 price-range-search POST Controller 到 add-to-cart GET Controller 和 add-to-cart GET Controller 方法只返回 back() 到请求来自的页面。在这种情况下,问题就出现了。它无法将 back() 返回到后控制器。那么可能的解决方案是什么!

这实际上不是答案。但是@rits 和@Mohamed Ahmed 都希望看到这两个路由和控制器方法的代码。但是在评论中无法提供信息,这就是为什么发布作为答案。很抱歉。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-19
    • 2014-07-14
    • 2014-05-04
    • 2021-07-22
    • 2021-07-19
    • 2017-04-12
    • 2020-03-24
    • 2018-07-07
    相关资源
    最近更新 更多