检查用户是否已在 Laravel 中注册并确认

Checking if a user is already registered and confirmed in Laravel

我需要检查用户是否已经注册并通过电子邮件确认。

现在,我知道 Laravel 已经检查用户是否已经存在。

我只需要输入如下内容:

if($found_user->exists && $found_user->confirmed == 1) 
{
//do this
}else if($found_user->exists && $found_user->confirmed == 0)
{
//do that
}

我可以在 RegisterController 函数 create(array $data) 中轻松地做到这一点,但我感觉它不是放置函数的正确位置,因为一旦它被调用已验证。

有合适的地方放还是应该留在create中?

您需要阅读 Protecting Routes

您可以将 auth 中间件附加到您要保护的路由,或者直接在控制器的 __construct

Laravel ships with an auth middleware, which is defined at Illuminate\Auth\Middleware\Authenticate

如果您想要 own Auth middleware 您可以输入以下内容

php artisan make:middleware MyOwnAuth

通过这种方式,您可以在您需要的页面上检查用户是否已确认

我决定放在函数里了

create(array $data){
    $this->check_user_confirmation($data["email"])
...
}

虽然我知道某个地方有一个功能可以检查用户是否已经存在,并且最好在那里进行检查,但它可能是我不喜欢修改的应用程序核心的一部分任何框架。

如果其他人有更好的主意,我仍然开放。

你的更好的版本 Laravel 有中间件功能,中间件的基本概念是做限制之类的事情,而不像过滤器一样将请求传递给服务器。

<?php

namespace App\Http\Middleware;

use Closure;

class CheckConfirmedUser {
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     *
     * @return mixed
     */
    public function handle( $request, Closure $next ) {
        if ( $found_user->exists && $found_user->confirmed == 1 ) {
            //do this
        } else if ( $found_user->exists && $found_user->confirmed == 0 ) {
            //do that
        }

        return $next( $request );
    }
}

您需要查询 $found_user