Laravel:如何重定向到帖子页面

时间:2019-08-05 08:21:23

标签: php laravel redirect post routes

我有一个两步验证表单,这意味着第一个用户输入(在ValidateTicketData-Controller中)得到了验证,如果一切正确,您将进入下一个表单页面,我将通过

进入该页面。
Route::post('/{locale?}/ticket/{cat?}', 'TicketController@store')->name('validation');

问题:如果用户未通过验证,则需要在第二页上载文件。
如果是这种情况,验证器类将立即重定向,因为它是路由后的路由,因此无法正常工作。

所以我创建了一条这样的获取路线:

Route::get('/{locale?}/ticket/{cat?}', 'TicketController@store')->name('validation');

并将其放入store方法中

$ticketData = $request->validated();
if ($request->isMethod('get')) {
    $error = 'No pdf-file has been attached.';
    return view ('/validation', compact('ticketData', 'cat', 'error'));
}

我将其放入store方法中,因为如果用户不将文件附加在第二页上,就会在这里重定向用户。

但是如果我现在尝试发送表单而不附加文件,则会收到消息I've redirected too many times

由于Validation类会自动执行此操作,因此我找不到找到如何使用第一页中经过验证的输入重定向到“ validation”页面的解决方案(因为它将再次显示)。

编辑

我将获取路线更改为此:

Route::get('/{locale?}/ticket/{cat?}', function() {
  $error = 'no pdf tho.';
  return view('/validation', compact('error'));
});

并在视图中显示$ error(如果不为空)。这行得通,但我仍然不知道如何从第一页获取输入数据。

我也有$locale

的中间件
public function handle($request, Closure $next)
{
   $locale = $request->segment(1);
   app()->setLocale($locale);
   return $next($request);
}

有时候似乎不允许我重定向,我不太了解

2 个答案:

答案 0 :(得分:1)

据我了解,问题在于第一次POST之后,您将进入一个新页面,验证可能会失败,如果验证失败,它将重定向回自身(重新加载)-这将失败,因为POST中的数据来自现在缺少第一步。

这是常见的情况,标准解决方案是使用PRG - Post/Redirect/Get。这意味着在成功处理 P OST之后,您的代码 R 将(带有 G ET请求)重定向到新页面,而不仅仅是返回该页面的内容。这样,如果用户在新页面上点击重新加载-或者在该新页面上的验证失败-它将重新加载(使用GET)该新页面,而无需重新提交POST。

在您的情况下,这看起来像(我使用简化的URI来使事情保持简单,并且我可能混淆了您的控制器方法):

LeadingPosition

现在,您的// GET the first page where user enters input Route::get('/first-page-form', 'TicketController@showFirstPage'); // Handle that data being POSTed Route::post('/first-page-processing', 'TicketController@store')->name('validation'); 方法进行了验证,并假设一切都通过了,只是返回一个视图,而是:

TicketController@store

您将需要GET路由来处理该问题:

public function store(...) {
    // Validation code ...
    //
    // Assuming validation OK, save POSTed data to DB, or maybe
    // to session ...
    //
    // All done - redirect to a new page with a GET.
    return redirect('/next-page-form');
}

如果验证失败,它将仅通过GET重定向回到// GET the next page where user enters input Route::get('/next-page-form', 'TicketController@showNextPage'); // And handle the next data being POSTed Route::post('/next-page-processing', 'DatenTicketController@store');

答案 1 :(得分:0)

您可以使用laravel Validation Class进行此操作,方法是进行验证以处理验证响应。您可以按照以下代码进行操作。

$validator = \Validator::make($request->all(), [
    'validation1' => 'required',
    'validation2' => 'required',
    'validation3' => 'required',
]);

if ($validator->fails())
{
    redirect()->route('your route')->with('error',$validator->errors()->all());
    // or you can return response json for your some ajax or api calls
    return response()->json(['errors'=>$validator->errors()->all()]);
}

//do whatever on validation passes
// store data to database
return response()->json(['success'=>'Record is successfully added']);

您可以在manually-creating-validators

上从官方文档中阅读该文档。