您好,我想检查用户电子邮件是否仅在控制器中的一个功能中进行验证,我不想在中间件或路径中设置检查,如下所示:
public function __construct()
{
$this->middleware('verified');
}
因为控制器可供访客访问,所以控制器中只有一项功能(创建)需要验证电子邮件和用户登录,我该怎么做,谢谢
public function create()
{
// checking if authenticated but need to check if email verified also
if (Auth::check()) {
// Get the currently authenticated user...
$user = Auth::user();
// get the list of states
$states = State::orderBy('name', 'asc')->get();
// get all cities by state_id
$state_id = '1';
$cities = City::where('state_id', $state_id)->orderBy('name', 'asc')->get();
return view('pages.create_product', ['user' => $user, 'states' => $states, 'cities' => $cities]);
} else {
return redirect()->route('login');
}
}
我建议你要么在web.php
路由文件如下:
Route::get('example/create', ExampleController@create)->middleware('verified');
或者在控制器构造函数中执行此操作,但对于如下所示的特定方法:
public function __construct()
{
$this->middleware('verified')->only('create');
}
如果您确实想在控制器方法中执行此操作,可以使用此代码进行检查。
$user = Auth::user();
$user->hasVerifiedEmail();
并且不要忘记包括MustVerifyEmail
您的界面User
model.
<?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;
// ...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)