Laravel 8 表单请求验证重定向到索引页面而不是同一页面并显示错误

Laravel 8 Form Request Validation Redirect to Index page instead same page and show error

在本地主机上一切正常,但是当我将应用程序部署到服务器时无法正常工作。如果表单请求验证失败而不是将我带回同一页面并显示错误,它会将我重定向到索引页面。

config.blade.php

<form method="POST" action="{{ route('config.update', $config->id) }}">
   @csrf
   @method('PUT')
   <div class="form-group row">
      <div class="col">
         <label class="col-form-label">Name</label>
         <input id="name" type="text" class="form-control" name="name" value="{{ $config->name }}" required>
      </div>
   </div>
   <div class="form-group row mt-3">
      <div class="col">
         <label class="col-form-label text-md-right">Address</label>
         <input id="address" type="text" class="form-control" name="address" value="{{ $config->address }}">
      </div>
   </div>
   <div class="form-group row mt-3">
      <div class="col">
         <label class="col-form-label text-md-right">Phone</label>
         <input id="phone" type="tel" class="form-control" name="phone" value="{{ $config->phone }}" required>
      </div>
   </div>
   <div class="form-group row mt-3">
      <div class="col">
         <label class="col-form-label text-md-right">E-mail</label>
         <input id="email" type="email" class="form-control" name="email" value="{{ $config->email }}" required>
      </div>
   </div>
   <div class="form-group row mt-4 mb-0">
      <div class="col-md-12">
         <button type="submit" class="btn btn-primary button-full-width">Save changes</button>
      </div>
   </div>
</form>

web.php

Route::resource('/admin/config', 'Admin\ConfigController');

配置控制器

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Services\ConfigServices;
use App\Http\Requests\ConfigRequest;
use App\Models\Config;

class ConfigController extends Controller
{
    protected $configServices;

    public function __construct(ConfigServices $configServices) {
        $this->middleware('auth');
        $this->configServices = $configServices;
    }

    ...

    public function update(ConfigRequest $request, $id)
    {
        $config = $this->configServices->updateConfigById($request, $id);
        return redirect()->back();
    }

    ...

}

ConfigRequest - 这就是问题所在

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ConfigRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required|string|max:255',
            'address' => 'nullable|string|max:255',
            'phone' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/|min:9|max:15',
            'email' => 'required|email:rfc',
        ];
    }
}

表单请求 return 索引页面而不是同一页面。在本地主机上一切正常,但是当我将应用程序部署到服务器时出现问题。 当表单请求上的数据验证正确时 return 我回到同一页面并显示成功,但是当表单请求失败时由于某种原因将我的重定向到索引页面。

Laravel 8 中出现问题,此代码在以前的 Laravel 版本中运行良好。

有人可以帮帮我吗?

在您的自定义请求中,您需要:

/**
 * The URI that users should be redirected to if validation fails.
 *
 * @var string
 */
protected $redirect = '/dashboard';

/**
 * The route that users should be redirected to if validation fails.
 *
 * @var string
 */
protected $redirectRoute = 'dashboard';

您可以在 docs 中找到更多信息。

在旧版本 Laravel 的文档中,这些属性不存在。