【问题标题】:Laravel 5.2 Post 302 redirect to GETLaravel 5.2 Post 302 重定向到 GET
【发布时间】:2016-09-15 15:06:47
【问题描述】:

我的问题是我的 POST 请求在错误 302 后总是重定向到 GET

这是我的路线文件:

Route::auth();
...
Route::post('/personnalite/creer', 'PersonnaliteController@creerPost')->name('personnaliteCreerPost');
Route::get('/personnalite/creer', 'PersonnaliteController@creerGet')->name('personnaliteCreerGet');
...

当我在发布之前删除 get 路由时,laravel 路由失败。 我可以在我的网络路由器开发工具中看到重定向之前的发布请求

这是我的 middelware,它在“web”middelwareGroups 中注册。 它只是检查用户角色是否允许路由(ACL 规则在配置文件中注册)

class MyAclMiddleware {
    public function handle($request, Closure $next) {
        $myAcl = App::offsetExists('MyAcl') ? App::make('MyAcl') : null;
        if($myAcl) {
            if(Auth::guest()) {
                $myAcl->setRole(0);
            } else {
                $myAcl->setRole(Auth::user()->role);
            }
            if($myAcl->isNotAllowed('route.' . Route::getRoutes()->match($request)->getName())) {
                return redirect()->route('erreur', ['id' => 0]);        
            }
        }
        return $next($request);
    }
}

这是我的表单刀片模板:

@extends('layouts.app')
@section('content')
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <div class="panel panel-default">
                <div class="panel-heading">Créer une personnalité politique</div>
                <div class="panel-body">
                    {{ @Form::open(['route' => 'personnaliteCreerPost', 'files' => true]) }}
                        @include('personnalite.subviews.formInfosGenerales')
                        {{ Form::submit('Créer') }}
                    {{ @Form::close() }}
                    @include('personnalite.subviews.listePersonnalites')
                </div>
            </div>
        </div>
    </div>
@enduction

其他开箱即用的 laravel 表单和登录控制器......工作正常。

谁能帮我解决这个问题?

【问题讨论】:

  • 如果oyu掷骰子('here');在 creerPost 方法中是否显示过?
  • 还可以尝试在 Form::open 调用中明确将 'method' => 'post' 放入参数中,看看是否有任何改变
  • 对于最近升级 Laravel 的用户,请检查控制器中中间件的语法。它可能需要更新。

标签: php redirect laravel-5 routing http-status-code-302


【解决方案1】:

解决了!

问题是由于我的控制器中的表单验证逻辑错误(测试不足 => 没有表单验证 => 重定向到以前的表单)

真丢脸

【讨论】:

    【解决方案2】:

    302 重定向是 Laravel 的(非 ajax)默认验证失败。您可以通过以下方式对其进行一些控制:

        $validator = Validator::make($request->all(), [
            'first_name' => 'required',
            'last_name' => 'required',
            'email' => 'required|email',
        ]);
    
        if ($validator->fails()) {
            $errors = $validator->errors();
            return redirect()->back()->withErrors($errors)->withInput();
        }
    

    通过捕获故障然后手动重定向,您可以确定返回到您的页面的内容。此外,除非您有一些代码来显示它们,否则这些错误不会显示在您的页面上 - 它们保存在会话变量中。所以看起来你只是在一些奇怪的循环路线中返回页面,而实际上有错误要显示。复数 withErrors 对一系列验证错误有好处,可以像这样显示在刀片​​模板中的相应字段下:

    @if ($errors->has('first_name'))
        <span class="text-danger">{!! $errors->first('first_name') !!}</span>
    @endif
    

    然后你的页面顶部有这样的内容:

    @if (session('error'))
        <div class="alert alert-danger">
            {{ session('error') }}
       </div>
    @endif
    @if (session('success'))
        <div class="alert alert-success">
            {{ session('success') }}
        </div>
    @endif
    

    捕获从控制器发送的自定义错误消息,例如:

    return redirect()->back()->withSuccess('Profile updated');
    

    return redirect()->back()->with("error","New Password does not match the password confirmation field. Please make sure they are the same.");
    

    注意“errors”和“error”之间的一个字母区别。如果你打错了,那可能会让你大吃一惊。

    为了完整起见,状态 422 是您通过对控制器的 ajax 调用从验证失败中得到的结果。 Laravel 为你做了这么多!

    【讨论】:

      【解决方案3】:

      这对于遇到相同问题的其他用户可能有用,但上述解决方案无法解决:

      确保表单字段名称例如 &lt;input type="text" name="document_name"&gt; (document_name) 与模型中声明的规则字段名称匹配。
      public static $rules = ['document_name' =&gt; 'required|string'];
      它不会抛出任何错误,日志中什么也没有,它只是重定向到表单,因此很难找到问题。

      对我来说,这就是问题所在,与您的原因“糟糕的表单验证逻辑”不谋而合。

      【讨论】:

        猜你喜欢
        • 2017-10-20
        • 2015-07-29
        • 2019-04-22
        • 1970-01-01
        • 1970-01-01
        • 2013-07-10
        • 1970-01-01
        • 2020-07-05
        • 1970-01-01
        相关资源
        最近更新 更多