Laravel 5.2 需要一个实现默认身份验证驱动程序/“多重身份验证"的示例.现在需要做很多工作

Laravel 5.2 need an example which implements default Authentication Drivers / quot;Multi-Authquot;. which needs lots of works right now as(Laravel 5.2 需要一个实现默认身份验证驱动程序/“多重身份验证的示例.现在需要做很多工作) - IT屋-程序员软
本文介绍了Laravel 5.2 需要一个实现默认身份验证驱动程序/“多重身份验证"的示例.现在需要做很多工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

身份验证驱动程序/多重身份验证"

Authentication Drivers / "Multi-Auth"

在 laravel 5.2 发布之前,据说多重身份验证支持开箱即用.但是没有任何示例代码显示如何使用不同的驱动程序进行身份验证和路由.所以我需要帮助使用默认的 laravel 5.2 设置多重身份验证

as prior to release of laravel 5.2 it is stated that multi auth suppots out of the box. but there is no any example codes showing how to authenticate using different drivers with routes. So I need help setting up the multi-auth using default laravel 5.2

推荐答案

创建两个新模型:AppAdminAppUser.更新config/auth.php:

Create two new models: AppAdmin and AppUser. Update config/auth.php:

return [
    'defaults' => [
        'guard' => 'user',
        'passwords' => 'user',
    ],
    'guards' => [
        'user' => [
            'driver' => 'session',
            'provider' => 'user',
        ],
        'admin' => [
            'driver' => 'session',
            'provider' => 'admin',
        ],
    ],
    'providers' => [
        'user' => [
            'driver' => 'eloquent',
            'model' => 'AppUser',
        ],
        'admin' => [
            'driver' => 'eloquent',
            'model' => 'AppAdmin',
        ],
    ],
    'passwords' => [
        'user' => [
            'provider' => 'user',
            'email' => 'auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ],
        'admin' => [
            'provider' => 'admin',
            'email' => 'auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ]
    ]
];

在 kernel.php 中

In kernel.php

    protected $middleware = [
        IlluminateFoundationHttpMiddlewareCheckForMaintenanceMode::class,
        IlluminateSessionMiddlewareStartSession::class,
        IlluminateViewMiddlewareShareErrorsFromSession::class
    ];

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            AppHttpMiddlewareEncryptCookies::class,
            IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,

            //AppHttpMiddlewareVerifyCsrfToken::class,
        ],

        'api' => [
            'throttle:60,1',
        ],
    ];

在Route.php中设置下面的代码和测试

and in Route.php set below code and test

    Route::get('/login', function() {
        $auth = auth()->guard('admin');

        $credentials = [
            'email' =>  'admin@gmail.com',
            'password' =>  'password',
        ];

        if ($auth->attempt($credentials)) {
            return redirect('/profile');
        } 
    });



    Route::get('/profile', function() {
            if(auth()->guard('admin')->check()){
                 print_r(auth()->guard('admin')->user()->toArray());
            } 

            if(auth()->guard('user')->check()){
                print_r(auth()->guard('user')->user()->toArray());
            }
        });

这篇关于Laravel 5.2 需要一个实现默认身份验证驱动程序/“多重身份验证"的示例.现在需要做很多工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

In PHP how can you clear a WSDL cache?(在 PHP 中如何清除 WSDL 缓存?)
failed to open stream: HTTP wrapper does not support writeable connections(无法打开流:HTTP 包装器不支持可写连接)
Stop caching for PHP 5.5.3 in MAMP(在 MAMP 中停止缓存 PHP 5.5.3)
Caching HTTP responses when they are dynamically created by PHP(缓存由 PHP 动态创建的 HTTP 响应)
Memcached vs APC which one should I choose?(Memcached 与 APC 我应该选择哪一个?)
What is causing quot;Unable to allocate memory for poolquot; in PHP?(是什么导致“无法为池分配内存?在 PHP 中?)