• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

如何在Laravel 5.6中设置多个身份验证

php 搞代码 4年前 (2022-01-21) 21次浏览 已收录 0个评论

在本文中,我们将介绍Laravel 5.6中的多个身份验证。这里中的多个身份验证只是针对多个用户模型进行身份验证的过程。

在以下部分中,我们将演示Laravel的内置身份验证功能。更具体地说,我将向您展示如何验证管理员用户和普通用户。

第1步:Laravel设置

使用composer设置Laravel项目。

composer create-project --prefer-dist laravel/laravel project-name

第2步:数据库配置

打开.env文件并在文件中设置数据库凭据。

DB_DATABASE= database-nameDB_USERNAME= rootDB_PASSWORD= database-password

第3步:身份验证

要使用Laravel的内置身份验证系统进行注册和登录,只需运行以下命令:

php artisan make:auth

第4步:设置模型和迁移

为管理员Admin创建和设置模型和迁移:

php artisan make:model Admin -m

要为Admin设置模型,请转到app / Admin.php并使用以下代码更新代码:

/** * Remove 'use Illuminate\Database\Eloquent\Model;' */use Illuminate\Notifications\Notifiable;use Illuminate\Foundation\Auth\User as Authenticatable;class Admin extends Authenticatable{    use Notifiable;// The authentication guard for admin    protected $guard = 'admin';     /**      * The attributes that are mass assignable.      *      * @var array      */    protected $fillable = [        'email', 'password',    ];     /**      * The attributes that should be hidden for arrays.      *      * @var array      */    protected $hidden = [        'password', 'remember_token',    ];

要为Admin设置迁移表,请转到database / migration / *** _ create_admins_table.php并使用以下代码更新代码:

//{    Schema::create('admins', function (Blueprint $table) {            $table->increments('id');            $table->string('email')->unique();            $table->string('password');            $table->rememberToken();            $table->timestamps();    }); }//

第5步:设置管理员控制器

要为Admin创建控制器,请运行以下命令:

php artisan make:controller AdminController

要设置控制器,请转到app / Http / Controllers / AdminController.php并使用以下代码更新代码:

class AdminController extends Controller{    /**     * Create a new controller instance.     *     * @return void     */    public function __construct()    {        $this->middleware('auth:admin');    }    /**     * Show the application dashboard.     *     * @return \Illuminate\Http\Response     */    public function index()    {        return view('admin');    }}

第6步:为管理员用户设置登录控制器

要创建Login控制器,请运行以下命令:

php artisan make:controller Auth/AdminLoginController

要设置登录控制器,请转到app / Http / Controllers / Auth / AdminLoginController.php并使用以下代码更新代码:

use Illuminate\Http\Request;use App\Http\Controllers\Controller;use Illuminate\Foundation<strong>(本文来源gaodai#ma#com搞@@代~&码网</strong><pre>搞代gaodaima码

\Auth\AuthenticatesUsers;use Illuminate\Support\Facades\Auth;class AdminLoginController extends Controller{ /** * Show the application’s login form. * * @return \Illuminate\Http\Response */ public function showLoginForm() { return view(’auth.admin-login’); } protected function guard(){ return Auth::guard('admin'); } use AuthenticatesUsers; /** * Where to redirect users after login. * * @var string */ protected $redirectTo = '/admin/dashboard'; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('guest:admin')->except('logout'); }}


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:如何在Laravel 5.6中设置多个身份验证

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址