在我的控制器中执行Mailer函数以实现联系我们功能后,我遇到了一个奇怪的问题:laravel reDirect().它有时会返回服务器错误500,并且日志(log)显示
"[2024-02-25 16:53:40]Production.Error:未指定应用程序加密密钥.{"Exception":"[Object](Illuminate\Encryption\MissingAppKeyException(code:0):未指定应用程序加密密钥.在C:\xampp\htdocs\globus-transfers\vendor\laravel\framework\src\Illuminate\Encryption\EncryptionServiceProvider.php:79)"
这是无稽之谈,因为我有一个可以正常工作的应用程序密钥.如果我使用视图而不是重定向,这个问题就解决了,但我不明白为什么
这是我遇到问题的代码:
<?php
namespace App\Http\Controllers;
use App\Mail\ContactMail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
class ContactController extends Controller
{
public function show()
{
return view('contact');
}
public function send(Request $request)
{
// Validate the input
$request->validate([
'contact_full_name' => 'required|string|max:255',
'contact_email' => 'required|email|max:255',
'contact_phone' => 'required|string|max:20',
'contact_message' => 'required|string',
]);
// Send mail to the company
Mail::to(env('MAIL_FROM_ADDRESS'))->send(new ContactMail($request));
// return view('contact', ['message' => 'Your message has been sent. We will get back to you soon.']);
return redirect()->route('contact.show')->with('message', 'Your message has been sent. We will get back to you soon.');
}
}
如果我使用查看功能,一切都很顺利,唯一的缺点是用户能够在页面刷新时重新提交数据,我不希望这样.如果我注释掉邮件行重定向没有问题,那么邮件和重定向方法之间的关系是有问题的……
这也是我的表单,它将请求发送到ContactController:
<form method="post" action="{{ route('contact.send') }}" class="lg:col-span-4 space-y-4">
@csrf
<div class="flex flex-col space-y-2">
<label for="contact_full_name">YOUR NAME *</label>
<input type="text" name="contact_full_name" id="contact_full_name" class="border-2 w-full rounded p-2" required>
</div>
<div class="flex flex-col space-y-2">
<label for="contact_email">EMAIL ADDRESS *</label>
<input type="contact_email" name="contact_email" id="contact_email" class="border-2 w-full rounded p-2" required>
</div>
<div class="flex flex-col space-y-2">
<label for="contact_phone">PHONE *</label>
<input type="text" name="contact_phone" id="contact_phone" class="border-2 w-full rounded p-2" required>
</div>
<div class="flex flex-col space-y-2">
<label for="contact_message" class="">MESSAGE *</label>
<textarea name="contact_message" class="input-text w-full border-2 p-2 rounded" id="contact_message" rows="5">{{old('contact_message')}}</textarea>
</div>
<div class="text-end">
<button type="submit" class="py-3 px-5 bg-amber-500 transition hover:bg-amber-600 text-white rounded">SEND MESSAGE</button>
</div>
</form>
以下是我的路由:
Route::get('/contact', [ContactController::class, 'show'])-
>name('contact.show');
Route::post('/contact', [ContactController::class, 'send'])-
>name('contact.send');
除了错误500之外,我还必须提到错误419页面偶尔也会过期,但Mailer在所有情况下都工作得很好,没有任何问题,只是reDirect()有时会失败.确实很奇怪.