所以有一个类似的问题...
StackOverflow::Route [user.verification.notice] not defined / Override EnsureEmailIsVerified?
当使用多个守卫时,您可以在
中进行一些守卫重定向
App\Middleware\Authenticate.php
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
if (Arr::first($this->guards) === 'admin') {
return route('admin.login');
}
if (Arr::first($this->guards) === 'user') {
return route('user.login');
}
return route('login');
}
}
您可以将所有验证路由添加到您的 web.php 文件并更改命名路由。
所有的认证路由都可以在
中找到
Illuminate\Routing\Router.php
\
/**
* Register the typical authentication routes for an application.
*
* @param array $options
* @return void
*/
public function auth(array $options = [])
{
// Authentication Routes...
$this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
$this->post('login', 'Auth\LoginController@login');
$this->post('logout', 'Auth\LoginController@logout')->name('logout');
// Registration Routes...
if ($options['register'] ?? true) {
$this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
$this->post('register', 'Auth\RegisterController@register');
}
// Password Reset Routes...
if ($options['reset'] ?? true) {
$this->resetPassword();
}
// Email Verification Routes...
if ($options['verify'] ?? false) {
$this->emailVerification();
}
}
/**
* Register the typical reset password routes for an application.
*
* @return void
*/
public function resetPassword()
{
$this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
$this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
$this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
$this->post('password/reset', 'Auth\ResetPasswordController@reset')->name('password.update');
}
/**
* Register the typical email verification routes for an application.
*
* @return void
*/
public function emailVerification()
{
$this->get('email/verify', 'Auth\VerificationController@show')->name('verification.notice');
$this->get('email/verify/{id}/{hash}', 'Auth\VerificationController@verify')->name('verification.verify');
$this->post('email/resend', 'Auth\VerificationController@resend')->name('verification.resend');
}
因此,您可以手动将这些添加到您的 web.php 路由文件中,而不是使用 Auth::routes(),然后给它们命名路由。
注意:更改命名路线后,您需要在视图中正确引用它们。
它首先会抱怨的是通知邮件引用了默认命名路由...
您可以按照此处的示例,在验证邮件过程和忘记密码密码的过程中覆盖它。
Forgot Password Custom Named Route and Email
要实现这一点,您必须通过创建两个覆盖两个默认通知的自定义通知来覆盖电子邮件通知。
你可以模拟文件中的 laravel 默认结构
Illuminate\Auth\Notifications\VerifyEmail.php
Illuminate\Auth\Notifications\ResetPassword
一旦您创建了 2 个通知邮件。
例如
php artisan make:notification MailEmailVerificationNotification
在 App\Notifications\MailEmailVerificationNotification 中创建一个文件,该文件有效地复制了 Illuminate\Auth\Notifications\VerifyEmail.php 文件
您将该方法添加到您的模型中。 Laravel 默认为 User 但如果您使用具有多个租户身份验证的自定义守卫,您可以将其应用于您的相关模型。
然后你的模型上会有以下内容
/**
* Send the password reset notification.
* App\Notifications\MailResetPasswordNotification.php
*
* @param string $token
* @return void
*/
public function sendEmailVerificationNotification()
{
$this->notify(new MailEmailVerificationNotification());
}
走这条路更好,因为你覆盖了 Laravel 的默认逻辑,但你不编辑任何 Laravel 特定的文件,这意味着它们在更新 Laravel 时不会被覆盖,并且只会在设计发生变化时受到影响,比如最近的举动将 Laravel UI 提取到自己的包中,这在密码重置路线上略有改变。
您可能会注意到我们更改了 App\Middleware\Authenticate 文件...此文件不是供应商文件的一部分,虽然作为基本安装的一部分提供给您,但它留给您更改更新和更改...我们所做的更改只是为了容纳警卫,而不是允许多租户或不在应用程序中的广泛更改。
对于任何人,我希望这会有所帮助,并且我在旅途中学习了这一点,并希望在我忘记时参考这一点,并希望它可以帮助任何走类似道路的人。