在我的控制器中执行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()有时会失败.确实很奇怪.

推荐答案

这就是我目前的解决方法,它工作得很好,我使用view()helper函数而不是reDirect()在表单提交后刷新联系人页面.如果用户再次刷新页面,它将要求他重新提交,但由于这个原因,他的数据将存储在会话中,如果重新提交的数据与存储在会话中的数据相同,则返回错误:

    <?php

    namespace App\Http\Controllers;

    use App\Mail\ContactMail;
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Mail;
    use Illuminate\Support\Facades\Session;


    class ContactController extends Controller
    {
    public function show()
    {
        return view('contact');
    }

    public function send(Request $request)
    {
        // Check if the order has already been confirmed
        if (
            session()->has('message_sent') && 
            session('contactData.contact_full_name') == $request- 
   >input('contact_full_name') &&
            session('contactData.contact_email') == $request- 
   >input('contact_email') &&
            session('contactData.contact_phone') == $request- 
   >input('contact_phone') &&
            session('contactData.contact_message') == $request- 
   >input('contact_message')
        ) {
            return response()->json(['error' => 'Message has already been 
    sent.'], 400);
        }
        // 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',
        ]);

        // Mark the order as confirmed in the session
        session(['message_sent' => true]);
        Session::put('contactData', [
            'contact_full_name' => $request->input('contact_full_name'),
            'contact_email' => $request->input('contact_email'),
            'contact_phone' => $request->input('contact_phone'),
            'contact_message' => $request->input('contact_message')
        ]);

        // 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.']);
    }
    }

Laravel相关问答推荐

如何更改LIVE上的Filament占位符内容?

警告:构建Reaction App(VITE)后无法解码下载的字体警告

在postgres/mysqlс中,我可以定义基于json字段的唯一索引吗?

如何在 Laravel Eloquent Collection 数组之前添加一个键值对?

.gitignore 不忽略文件夹

Laravel 5.1 是否与 PHP 7 兼容

Laravel 5.5 在迁移文件中设置整数字段的大小

为什么人们将 .env 放入 gitignore?

Laravel 监听器监听多个事件

Laravel whereDoesntHave() - 多个 OR 条件

使用'with'时,Laravel belongsTo 返回 null

Laravel Nova - 重新排序左侧导航菜单项

如何利用 StorageFacade 的 Filesystem 类的 glob 方法?

如何在 Laravel 中使用旧输入重定向?

Laravel 分页方法不适用于 map 集合?

Laravel 迁移命名约定

遍历 Laravel 控制器中的结果集

Laravel 排序集合,然后按键

Laravel 5 Auth注销不起作用

有没有办法让表名自动添加到 Eloquent 查询方法中?