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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?