Laravel邮箱验证添加未登录的提示跳转

通过中间件判定未登录跳转登录页并提示内容。在中间件和系统的两个路由中添加规则定义。

创建中间件

app/Http/Middleware/RedirectIfNotVerifiedAndNotLoggedIn.php

<?php
// app/Http/Middleware/RedirectIfNotVerifiedAndNotLoggedIn.php
namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Route;

class RedirectIfNotVerifiedAndNotLoggedIn
{
    public function handle($request, Closure $next)
    {
        if (!$request->user() && Route::currentRouteName() === 'verification.verify') {
            return Redirect::route('login')->with('status', '验证邮箱前请先登录');
        }

        return $next($request);
    }
}

app/Http/kernel.php添加路由控制

 protected $routeMiddleware = [
        'redirectIfNotVerifiedAndNotLoggedIn' => \App\Http\Middleware\RedirectIfNotVerifiedAndNotLoggedIn::class,
    ];

登录页login.blade.php添加提示代码


                        {{-- 显示消息 --}}
                        @if (session('status'))
                        <div class="alert alert-info">
                            {{ session('status') }}
                        </div>
                        @endif

路由routes/web.php添加路由控制

Route::get('/email/verify/{id}/{hash}', function (EmailVerificationRequest $request) {
    $request->fulfill();

    return redirect('/home');
})->middleware(['signed', 'throttle:6,1', 'redirectIfNotVerifiedAndNotLoggedIn'])->name('verification.verify');

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值