【问题标题】:Laravel 5 how to return to a POST request with validation errorsLaravel 5 如何返回带有验证错误的 POST 请求
【发布时间】:2017-03-14 10:18:09
【问题描述】:

我在 Laravel 中有一个 POST 路由:

Route::post('/quote-summary', 'QuoteController@quoteSummary')->name('quote-summary');

在这条路线的视图中,我有一个提交到另一条路线的表单:

Route::post('/proceed-to-payment', 'QuoteController@proceedToPayment')->name('proceed-to-payment');

这是我的 proceed-to-payment 控制器方法:

public function proceedToPayment(Request $request) {

    if(empty($request->get('tick_statement_of_fact'))) {
        return redirect()->route('quote-summary')->with('tick_statement_of_fact', 'I am so frustrated.');


    } else {
        return view('quote.payment', compact('date_cover_required', 'expiry_date'));
    }

}

quoteSummary() 方法:

public function quoteSummary(QuoteRequest $request)
    {

        //dd($request);
        // get the quote
        $quote_data = $this->getQuote($request);
        $quote = number_format($quote_data, 2, '.', ',');



        if(!empty($request->get('units'))) {
            $units = $request->get('units');
        }
        if(!empty($request->get('limit_of_indemnity'))) {
            $limit_of_indemnity = number_format($request->get('limit_of_indemnity'), 2, '.', ',');
        }
        if(!empty($request->get('title'))) {
            $title = $request->get('title');
        }


        // store data in the session so we can access from generated documents
        session(['units' => $units, 'limit_of_indemnity' => $limit_of_indemnity, 'insured_name' => $insured_name, 'title' => $title, 'first_name' => $first_name, 'last_name' => $last_name, 'contact_number' => $contact_number, 'email' => $email, 'quote' => $quote]);



        return view('quote.summary');

    }

我正在尝试重定向回quote-summary,但因为它是POST,所以我得到了MethodNotAllowedHttpException,因为重定向似乎正在执行GET

关于如何通过一些验证错误消息返回到我的 POST 路由有什么想法吗?

【问题讨论】:

  • 你为什么要回到一个帖子路线?您需要重新考虑您的应用程序结构。如果您需要向quote-summary 发布内容,那么您应该在quote-summary 之前添加一个中间步骤,该步骤接受发布数据,将其存储(会话、数据库、任何地方),然后将您发送到quote-summary,以便quote-summary是一条获取路线。
  • 我将返回到 post 路由,因为我正在验证该视图上的某些字段,如果验证失败,我需要返回该视图并打印出错误消息,正如我的代码中自我解释的那样.
  • @Styphon 是对的。听起来很奇怪。如果你的第一个帖子成功了,为什么你需要在一个不成功的帖子之后回到这个成功的帖子?
  • 因为第二个帖子有一个来自第一个帖子视图的表单,所以我需要返回第一个帖子视图。
  • 好的,我想这是前端(切换视图)的情况,所以你处理起来很简单。

标签: php laravel laravel-5 request


【解决方案1】:

同时为您的 quoteSummary 操作提供发布和获取路线,这正是您所追求的,即:

Route::post('/quote-summary', 'QuoteController@quoteSummary')->name('quote-summary'); Route::get('/quote-summary', 'QuoteController@quoteSummary')->name('quote-summary');

【讨论】:

    猜你喜欢
    • 2015-09-01
    • 2016-08-30
    • 2021-04-10
    • 2020-01-17
    • 1970-01-01
    • 2023-01-14
    • 2018-03-23
    • 2015-12-24
    • 1970-01-01
    相关资源
    最近更新 更多