如何修复 Laravel 5.7 中的 "Class signed does not exist" 错误?
How to fix "Class signed does not exist" error in Laravel 5.7?
我刚刚将我的 Laravel 项目从 5.6 更新到 5.7。我升级的主要原因是我需要将电子邮件验证添加到我的项目中。在我完成所有升级步骤并根据 Laravel 文档实施电子邮件验证后,我收到错误消息。所以导致错误的步骤是这样的:
我使用 1 条路径进行测试,在我的 ..\routes\web.php 文件中我有这行代码:
Route::get('dashboard', ['uses' => 'DashboardController@getDashboard'])->middleware('verified');
当我尝试去那条路线时,它确实将我重定向到 ..\views\auth\verify.blade.php 的视图。在那里我点击 link 发送验证邮件。我收到电子邮件,然后单击电子邮件中的按钮以验证我的电子邮件。它启动浏览器并开始导航我到某个地方,这就是它收到错误的时候:
Class signed does not exist
经过大量研究,我发现错误出在说明中说要创建的新 VerificationController.php 文件中,导致问题的代码行是:
$this->middleware('signed')->only('verify');
如果我注释掉这一行并再次单击我电子邮件中的按钮,那么它可以正常工作并且我的用户 email_verified_at 列会更新为日期时间戳。
下面是完整的VerificationController.pas,以防它能说明问题:
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\VerifiesEmails;
class VerificationController extends Controller
{
/*
|--------------------------------------------------------------------------
| Email Verification Controller
|--------------------------------------------------------------------------
|
| This controller is responsible for handling email verification for any
| user that recently registered with the application. Emails may also
| be re-sent if the user didn't receive the original email message.
|
*/
use VerifiesEmails;
/**
* Where to redirect users after verification.
*
* @var string
*/
protected $redirectTo = '/dashboard';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
$this->middleware('signed')->only('verify');
$this->middleware('throttle:6,1')->only('verify', 'resend');
}
}
看看Laravel Documentation on Signed URLs
我猜你在 $routeMiddleware
数组
中遗漏了这个条目
// In app\Http\Kernel.php
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
...
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
];
我在 API 电子邮件验证方面遇到了同样的问题,我不得不添加触发电子邮件发送的事件 app/Providers/EventServiceProvider。php
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
];
并覆盖 app/Http/Controllers/Auth/VerificationController.php 函数
/**
* Show the email verification notice.
*
*/
public function show()
{
}
/**
* Mark the authenticated user's email address as verified.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function verify(Request $request)
{
if ($request->route('id') == $request->user()->getKey() &&
$request->user()->markEmailAsVerified()) {
event(new Verified($request->user()));
}
return response()->json('Email verified!');
}
/**
* Resend the email verification notification.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function resend(Request $request)
{
if ($request->user()->hasVerifiedEmail()) {
return response()->json('User already have verified email!', 422);
}
$request->user()->sendEmailVerificationNotification();
return response()->json('The notification has been resubmitted');
}
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
$this->middleware('signed')->only('verify');
$this->middleware('throttle:6,1')->only('verify', 'resend');
}
我刚刚将我的 Laravel 项目从 5.6 更新到 5.7。我升级的主要原因是我需要将电子邮件验证添加到我的项目中。在我完成所有升级步骤并根据 Laravel 文档实施电子邮件验证后,我收到错误消息。所以导致错误的步骤是这样的:
我使用 1 条路径进行测试,在我的 ..\routes\web.php 文件中我有这行代码:
Route::get('dashboard', ['uses' => 'DashboardController@getDashboard'])->middleware('verified');
当我尝试去那条路线时,它确实将我重定向到 ..\views\auth\verify.blade.php 的视图。在那里我点击 link 发送验证邮件。我收到电子邮件,然后单击电子邮件中的按钮以验证我的电子邮件。它启动浏览器并开始导航我到某个地方,这就是它收到错误的时候:
Class signed does not exist
经过大量研究,我发现错误出在说明中说要创建的新 VerificationController.php 文件中,导致问题的代码行是:
$this->middleware('signed')->only('verify');
如果我注释掉这一行并再次单击我电子邮件中的按钮,那么它可以正常工作并且我的用户 email_verified_at 列会更新为日期时间戳。
下面是完整的VerificationController.pas,以防它能说明问题:
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\VerifiesEmails;
class VerificationController extends Controller
{
/*
|--------------------------------------------------------------------------
| Email Verification Controller
|--------------------------------------------------------------------------
|
| This controller is responsible for handling email verification for any
| user that recently registered with the application. Emails may also
| be re-sent if the user didn't receive the original email message.
|
*/
use VerifiesEmails;
/**
* Where to redirect users after verification.
*
* @var string
*/
protected $redirectTo = '/dashboard';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
$this->middleware('signed')->only('verify');
$this->middleware('throttle:6,1')->only('verify', 'resend');
}
}
看看Laravel Documentation on Signed URLs
我猜你在 $routeMiddleware
数组
// In app\Http\Kernel.php
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
...
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
];
我在 API 电子邮件验证方面遇到了同样的问题,我不得不添加触发电子邮件发送的事件 app/Providers/EventServiceProvider。php
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
];
并覆盖 app/Http/Controllers/Auth/VerificationController.php 函数
/**
* Show the email verification notice.
*
*/
public function show()
{
}
/**
* Mark the authenticated user's email address as verified.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function verify(Request $request)
{
if ($request->route('id') == $request->user()->getKey() &&
$request->user()->markEmailAsVerified()) {
event(new Verified($request->user()));
}
return response()->json('Email verified!');
}
/**
* Resend the email verification notification.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function resend(Request $request)
{
if ($request->user()->hasVerifiedEmail()) {
return response()->json('User already have verified email!', 422);
}
$request->user()->sendEmailVerificationNotification();
return response()->json('The notification has been resubmitted');
}
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
$this->middleware('signed')->only('verify');
$this->middleware('throttle:6,1')->only('verify', 'resend');
}