当我尝试使用 Laravel 提交表单时,我收到错误 500 Internal Server Error http://127.0.0.1:8000/sendEmail 是否有原因

问题描述 投票:0回答:1

我有一个联系表单,当用户提交时,会向网站管理员发送一封电子邮件,并向用户发送一份副本。但是,当我尝试提交时,出现错误:

错误:500内部服务器错误http://127.0.0.1:8000/sendEmail

在我的 Laravel 调试器上我得到:

未找到视图 [view.name]。用户/用户/文档/Laravel 应用程序/GitHub/scinov_africa/vendor/laravel/framework/src/Illuminate/View/FileViewFinder.php#137 InvalidArgumentException

我无法确定缺少哪个视图以及出现服务器错误的原因。

web.php 中的我的路线

<?php

use App\Http\Controllers\events\EventsController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\project\ProjectsController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/', function () { return view('welcome'); });

Route::get('/events', [EventsController::class, 'activities'])->name('events.activities');

Route::get('/coding-education', [ProjectsController::class, 'codingEducation'])->name('projects.codingEducation');
Route::get('/donate', [ProjectsController::class, 'donate'])->name('projects.donate');
Route::get('/donations', [ProjectsController::class, 'donations'])->name('projects.donations');
Route::get('/digital-exchange', [ProjectsController::class, 'digitalExchange'])->name('projects.digitalExchange');
Route::get('/contact-us', [ProjectsController::class, 'contactUs'])->name('projects.contact-us');
Route::post('/sendEmail', [ProjectsController::class, 'sendEmail'])->name('projects.sendEmail');

Route::get('/dashboard', function () { return view('dashboard'); })->middleware(['auth', 'verified'])->name('dashboard');

Route::middleware('auth')->group(function () {
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});

require __DIR__.'/auth.php';

我的表格

<form action="{{ route('projects.sendEmail') }}" method="post" role="form" class="php-email-form">

    @csrf
    <div class="row">
        <div class="col-md-6 form-group">
            <input type="text" name="name" class="form-control" id="name"
                placeholder="Your Name" required>
        </div>
        <div class="col-md-6 form-group mt-3 mt-md-0">
            <input type="email" class="form-control" name="email" id="email"
                placeholder="Your Email" required>
        </div>
    </div>
    <div class="form-group mt-3">
        <input type="text" class="form-control" name="subject" id="subject" placeholder="Subject"
            required>
    </div>
    <div class="form-group mt-3">
        <textarea class="form-control" name="message" rows="5" placeholder="Message" required></textarea>
    </div>
    <div class="my-3">
        <div class="loading">Loading</div>
        <div class="error-message"> Message not sent</div>
        <div class="sent-message">Message sent successfully!</div>
        
        @if (session('success'))
            <div class="alert alert-success">
                {{ session('success') }}
            </div>
        @endif
    </div>
    
    <div class="text-center">
        <br>
        <button type="submit" class="btn btn-primary ">Send Message</button>
    </div>
</form>

我的日志显示:

local.ERROR:未找到视图 [view.name]。 {“异常”:“[对象] (InvalidArgumentException(代码:0):未找到视图 [view.name]。位于 /用户/用户/文档/Laravel 应用程序/GitHub/scinov_africa/vendor/laravel/framework/src/Illuminate/View/FileViewFinder.php:137)

请帮忙。我处于开发模式,根本没有部署我的代码。我刚刚将代码放入名为 GitHub 的文件夹中,因此它不是来自 GitHub 的克隆应用程序。

我首先认为 validate.js 文件抛出错误是因为我的表单存在问题..我无法批准这一点。

// Function to submit the form data via AJAX
function php_email_form_submit(thisForm, action, formData) {
  // Send a POST request to the specified action URL with the form data
  fetch(action, {
    method: 'POST',
    body: formData,
    headers: {'X-Requested-With': 'XMLHttpRequest'} // Set the request header
  })
  .then(response => {
    // Check if the response status is OK (200)
    if (response.ok) {
      // If OK, return the response text (which may contain success or error information)
      return response.text();
    } else {
      // If not OK, throw an error with status, status text, and request URL information
      throw new Error(`${response.status} ${response.statusText} ${response.url}`);
    }
  })
  .then(data => {
    // After a successful response, remove the loading indicator
    thisForm.querySelector('.loading').classList.remove('d-block');

    // Check the response data for success ('OK') or an error message
    if (data.trim() == 'OK') {
      // If success, display the success message and reset the form
      thisForm.querySelector('.sent-message').classList.add('d-block');
      thisForm.reset();
    } else {
      // If an error occurred, throw an error with the received error message
      throw new Error(data ? data : 'Form submission failed and no error message returned from: ' + action);
    }
  })
  .catch((error) => {
    // Catch and handle any errors that occurred during the request
    displayError(thisForm, error);
  });
}

我还检查了渲染视图的函数

public function contactUs()
    {


        return view('contact_us');
    }

    public function sendEmail(Request $request)
    {
        $validatedData = $request->validate([
            'name' => 'required|string',
            'email' => 'required|email',
            'subject' => 'required|string',
            'message' => 'required|string',
        ]);

        // Send an email to the admin
        Mail::to('[email protected]')->send(new  AdminEmail($validatedData));

        // Send a response email to the user
        Mail::to($validatedData['email'])->send(new  UserResponseEmail($validatedData));

        // Redirect back with a success message
        return back()->with('success', 'Your message has been sent successfully.');
    }

我在 Mac 上使用 Laravel 10.22 PHP 8.28

laravel http-status-code-500
1个回答
0
投票

我已经确定我的代码出了什么问题。 AdminResponse 类未正确配置。在 content 方法中,返回的视图被标记为 view.name。看代码

 /**
 * Get the message content definition.
 */
public function content(): Content
{
    return new Content(
        view: 'view.name',
    );
}

我已使用刀片文件中的视图更新了返回的视图。现在我很好了。

© www.soinside.com 2019 - 2024. All rights reserved.