在本地主機上一切都很好,但是當我將應用程式部署到服務器時卻無法正常作業。如果表單請求驗證失敗而不是讓我回到同一頁面并顯示錯誤,它會將我重定向到索引頁面。
組態檔
<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 hljs-number">3">
<div hljs-string">">
<label hljs-string">">Address</label>
<input id="address" type="text" hljs-string">" name="address" value="{{ $config->address }}">
</div>
</div>
<div hljs-number">3">
<div hljs-string">">
<label hljs-string">">Phone</label>
<input id="phone" type="tel" hljs-string">" name="phone" value="{{ $config->phone }}" required>
</div>
</div>
<div hljs-number">3">
<div hljs-string">">
<label hljs-string">">E-mail</label>
<input id="email" type="email" hljs-string">" name="email" value="{{ $config->email }}" required>
</div>
</div>
<div hljs-number">4 mb-0">
<div hljs-number">12">
<button type="submit" hljs-string">">Save changes</button>
</div>
</div>
</form>
網頁.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',
];
}
}
表單請求回傳到索引頁而不是同一頁。在本地主機上一切正常,但是當我將應用程式部署到服務器時出現問題。當表單請求上的資料驗證正確時,將我回傳到同一頁面并顯示成功,但是當表單請求失敗時,由于某種原因將我的重定向到索引頁面。
Laravel 8 中出現了一個問題,這段代碼在之前的 Laravel 版本中運行良好。
有人能幫助我嗎?
uj5u.com熱心網友回復:
在您的自定義請求中,您需要:
/**
* 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';
您可以在檔案中找到更多資訊。
在舊版 Laravel 的檔案中,這些屬性不存在。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/380327.html
上一篇:將值附加到URL
