简体   繁体   English

验证然后重定向到Laravel中的后期路线

[英]Validating and then redirecting to a post route in Laravel

Is it possible to redirect the user to a POST route in Laravel. 是否可以将用户重定向到Laravel中的POST路由。 I have 2 forms. 我有两种形式。 form-one is sent to the Route containing form-two and form-two is sent to the final Route and then it is validated. form-one被发送到包含form-two的Route,并且form-two被发送到最终的 Route,然后它被验证。 If $validator->fails() for form-two is a truthy value at the final Route I want to send the user back to from-two but it is a POST Route . 如果$validator->fails()形式,二是在最后的路线我想给用户发送回从-两个 truthy值,但它是一个POST路线

Redirect::to('Form-Two')->withErrors($validator);

I tried using this but it failed maybe because it only works for Get routes. 我尝试使用它但它失败了可能是因为它只适用于Get路线。 One thing I thought of doing was to redirect the user to a Get Route and then post the data to form-two from that Get Route, but that sounds stupid. 我想做的一件事是将用户重定向到Get Route,然后将数据发布到来自Get Route的form-two ,但这听起来很愚蠢。 Is there any cleaner way to do this. 有没有更清洁的方法来做到这一点。 I'm a newbie. 我是新手。

Form Two: 表格二:

Route::post('form-two', array('before' => 'csrf', function()
{
    $formOneData= Input::all();
    $rules = array(...);

    $validator = Validator::make($formOneData, $rules);
    if ($validator->fails()) {
        return Redirect::to('Form-One')->withErrors($validator);
    }
}

Final Page: 最终页面:

Route::post('final', array('before' => 'csrf', function()
{
    $finalData = Input::all();
    $rules = array(...);

    $validator = Validator::make($finalData, $rules);
    if ($validator->fails()) {
        return Redirect::to('Form-Two')->withErrors($validator);
    }
}

This isn't particularly a Laravel issue, this requires you to understand how HTTP redirects work. 这不是特别的Laravel问题,这要求您了解HTTP重定向的工作原理。 When you send a redirect to a browser, you are sending the browser a 302 redirect with a URL to redirect to. 当您向浏览器发送重定向时,您正在向浏览器发送302重定向,其中包含要重定向到的URL。 Browsers will redirect making a GET request to the URL provided. 浏览器会将GET请求重定向到提供的URL。 You could potentially change the response code to 307 which asks the browser to do the redirect with the same method as was originally called with a security message but it is a bad idea to rely on this as it is implemented differently across browsers. 您可能会将响应代码更改为307,这会要求浏览器使用与最初使用安全消息调用的方法相同的方法执行重定向, 依赖于此,因为它在浏览器中的实现方式不同。 Also Laravel would require you to build a custom response object with your own headers. Laravel还要求您使用自己的标头构建自定义响应对象。

To keep your code compatible across browsers it is better practice to separate GET and POST logic. 为了使代码在浏览器之间保持兼容,最好将GET和POST逻辑分开。 This is why returning views directly from a POST route is generally a bad idea. 这就是为什么直接从POST路由返回视图通常是个坏主意。

The way I see this you really need to be looking to re-factor and rework how your forms work if you want the functionality you are aiming for. 我看到这个的方式,如果你想要你想要的功能,你真的需要重新考虑并重新设计表单的工作方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM