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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题