【问题标题】:Laravel | Redirect to verify email if not verified拉拉维尔 |如果未验证,则重定向以验证电子邮件
【发布时间】:2021-06-04 02:22:35
【问题描述】:

我是 Laravel 的新手。目前,当您访问一个页面并且您的帐户未经过验证时,什么都不会发生。我想这样做,如果您已登录并且您的电子邮件未验证,它会将您重定向到验证电子邮件页面,但前提是您已登录。我已经尝试但失败了。

我用这个做了一个中间件,但它似乎不起作用。

if (Auth::user() && ! Auth::user()->email_verified_at) {
        return redirect('auth/verify');;
}

这是我的 web.php

// Home Page
Route::get('/', function () {
 return view('home');
})->middleware('verifiedEmail');

// Private Page
 Route::get('/private', [HomeController::class, 'private']);


// Home Page
Route::get('/home', 'HomeController@index')->name('home');

// Shop Page
Route::get('/shop', 'ShopController@index')->name('shop');

/*
|===========================
| Other Routes
|===========================
*/

// Mailing
Route::get('/email', function() {
  Mail::to($request->user())->send(new MailableClass);
  return new WelcomeMail;
});
Route::get('/sendtestnews', [TestNewsController::class, 
'sendTestNotification']);


// Verification
Auth::routes(['verify' => true]);

【问题讨论】:

  • 显示路由以及控制器方法
  • 我不确定是否需要任何其他文件,所以我只做了一个中间件
  • 否则我们不能假设错误在哪里
  • 我将我的 web.php 添加到帖子中
  • 我在那里看不到 auth/verify 路由。您是否使用默认身份验证

标签: php laravel


【解决方案1】:

看起来你在重定向时使用了错误的网址

 return redirect()->route('verification.notice');

检查验证路由。运行以下命令

php artisan route:list

这将显示在web.phpapi.php 中定义的所有路由

| POST                                            | email/resend                                       | verification.resend                           | App\Http\Controllers\Auth\VerificationController@resend                           | web          |
|        |                                        |                                                    |                                               |                                                                                   | auth         |
|        |                                        |                                                    |                                               |                                                                                   | throttle:6,1 |
|        | GET|HEAD                               | email/verify                                       | verification.notice                           | App\Http\Controllers\Auth\VerificationController@show                             | web          |
|        |                                        |                                                    |                                               |                                                                                   | auth         |
|        | GET|HEAD                               | email/verify/{id}/{hash}                           | verification.verify                           | App\Http\Controllers\Auth\VerificationController@verify                           | web          |

更新

<form class="d-inline" method="POST" action="email/resend">
                        <input type="hidden" name="_token" value="o8OVdlN1Pvu5IrpKvtXybsLiYC0H8czZTW7AIf5Y">                        <button type="submit" class="btn btn-link p-0 m-0 align-baseline">click here to request another</button>.
                    </form>

如您所见,电子邮件/重新发送表单如上所示。 email/resend(route verification.resend ) 是 post 方法

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-11
    • 2018-05-29
    • 2018-08-19
    • 2022-01-20
    • 2017-10-22
    • 2019-03-08
    • 2019-09-09
    • 2018-04-02
    相关资源
    最近更新 更多