Laravel检查用户电子邮件是否已验证

时间:2020-08-24 23:47:41

标签: laravel

嗨,我想检查用户电子邮件是否仅在控制器的一项功能中得到验证,我不想在中间件或路由中设置检查,例如:

{
  "workbench.colorTheme": "One Dark Pro",
  "git.autofetch": true,
  "workbench.iconTheme": "material-icon-theme",
  "git.enableSmartCommit": true,
  "terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\cmd.exe",
  "[javascript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "[html]": {
    "editor.defaultFormatter": "vscode.html-language-features"
  },
  "javascript.updateImportsOnFileMove.enabled": "always",
  "[javascriptreact]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "liveServer.settings.donotShowInfoMsg": true,
  "editor.formatOnSave": true,
  "window.zoomLevel": 1,
  "vscode-w3cvalidation.validator-token": "Fri, 07 Aug 2020 07:35:05 GMT",
  "python.formatting.provider": "autopep8",
  "python.formatting.autopep8Args": [
    "--max-line-length",
    "79",
    "--experimental"
  ],
  "python.autoComplete.addBrackets": true,
  "python.autoComplete.extraPaths": []
}

因为访客可以访问控制器,所以控制器中只有一个功能(创建)需要验证电子邮件,并且用户登录后我该怎么做,谢谢

 public function __construct()
{
    $this->middleware('verified');
}

1 个答案:

答案 0 :(得分:2)

我建议您在web.php路由文件中执行以下操作:

Route::get('example/create', ExampleController@create)->middleware('verified');

或者在控制器构造函数中执行此操作,但要使用特定的方法,例如:

 public function __construct()
{
    $this->middleware('verified')->only('create');
}

如果您确实想在controller方法中执行此操作,则可以使用此代码进行检查。

$user = Auth::user();

$user->hasVerifiedEmail();

并且不要忘记在MustVerifyEmail模型中包含User接口。

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;

    // ...
}