验证然后重定向到Laravel中的发布路线 [英] Validating and then redirecting to a post route in Laravel

查看:72
本文介绍了验证然后重定向到Laravel中的发布路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将用户重定向到Laravel中的POST路由. 我有两种形式. form-one 发送到包含 form-two 的路由,而 form-two 发送到 final 路由然后进行验证.如果 form-two $validator->fails() final 路线上的真实值,我想将用户从两个位置重新发送回 POST路线.

Is it possible to redirect the user to a POST route in Laravel. 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. 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.

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

我尝试使用此方法,但失败了,可能是因为它仅适用于Get路线.我想做的一件事是将用户重定向到Get Route,然后将数据从该Get Route发布到 form-two ,但这听起来很愚蠢.有没有更清洁的方法可以做到这一点.我是新手.

I tried using this but it failed maybe because it only works for Get routes. 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. Is there any cleaner way to do this. I'm a newbie.

表格二:

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

最终页面:

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

推荐答案

这不是Laravel问题,这需要您了解HTTP重定向的工作方式.当您将重定向发送到浏览器时,您将向浏览器发送302重定向,其中包含要重定向的URL.浏览器会将GET请求重定向到提供的URL.您可能会将响应代码更改为307,该代码要求浏览器使用与最初使用安全消息调用的方法相同的方法进行重定向,但是在实现该方法时依靠它是一个坏主意不同的浏览器.而且Laravel会要求您使用自己的标头来构建自定义响应对象.

This isn't particularly a Laravel issue, this requires you to understand how HTTP redirects work. When you send a redirect to a browser, you are sending the browser a 302 redirect with a URL to redirect to. Browsers will redirect making a GET request to the URL provided. 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. Also Laravel would require you to build a custom response object with your own headers.

为保持代码在浏览器之间的兼容性,最好将GET和POST逻辑分开.这就是为什么直接从POST路由返回视图通常是个坏主意的原因.

To keep your code compatible across browsers it is better practice to separate GET and POST logic. This is why returning views directly from a POST route is generally a bad idea.

从我的角度来看,如果您想要目标功能,那么您确实需要寻求重构和重新设计表单的工作方式.

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.

这篇关于验证然后重定向到Laravel中的发布路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆