dongshenyu4638 2019-08-05 08:21
浏览 275
已采纳

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

I have a two step validation form, that means the first user input gets validated (within a ValidateTicketData-Controller) and if everything's correct you get to the next form page, which I get to by

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

Problem: On the second page the user is required to upload a file, if he doesn't the validation fails.
If this is the case the validator class immediately redirects which doesn't work since it's a post-route.

So I created a get route like this:

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

and put this in the store-method:

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

I put this into the store-method because this is where the user gets redirected if he won't attach a file on the second page.

But if I now try to send the form without attaching a file I get the message that I've redirected too many times.

I can't find a solution how to redirect to the 'validation`-page with the validated input from the first page (because it gets displayed again) since the Validation class does it automatically.

EDIT

I changed the get-route to this:

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

and displayed the $error (if it's not empty) in the view. This worked, but I still don't know how to get the input data from the first page.

I also have this middleware for the $locale

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

which seems to won't let me redirect sometimes, I don't really understand it

  • 写回答

2条回答 默认 最新

  • douxianji3367 2019-08-05 09:37
    关注

    As I understand it, the problem is that after the first POST, you land on a new page where validation may fail, and if it does, it redirects back to itself (reloads) - which will fail because the POSTed data from the first step is now missing.

    This is a common scenario, and the standard solution is to use PRG - Post/Redirect/Get. This means that after successfully processing a POST, your code Redirects (with a GET request) to a new page, rather than just returning that page's content. This way if the user hits reload on the new page - or if validation on that new page fails - it just reloads (with GET) that new page, without resubmitting the POST.

    In your case, that would look something like (I've used simplified URIs to keep things simple, and I may have mixed up your controller methods):

    // 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');
    

    Now your TicketController@store method does its validation, and assuming everything passes, do not just return a view, but instead:

    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');
    }
    

    You'll need a GET route to handle that:

    // 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');
    

    If validation fails, it will simply redirect with GET back to /next-page-form.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何实验stm32主通道和互补通道独立输出
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题