有谁能用例子解释拉勒维尔5.2多个八月?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (7)

我想验证一下用户管理形式user表和admin表分别。

我正在使用User模型是由Laravel提供的,完全相同。Admin...

我在auth.php

'guards' => [
    'user' =>[
        'driver' => 'session',
        'provider' => 'user',
    ],
    'admin' => [
        'driver' => 'session',
        'provider' => 'admin',
    ],
],  

提供者

'providers' => [
    'user' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],
    'admin' => [
        'driver' => 'eloquent',
        'model' => App\Admin::class,
    ]
],

路线

Route::group(['middleware' => ['web']], function () {
    //Login Routes...
    Route::get('/admin/login','AdminAuth\AuthController@showLoginForm');
    Route::post('/admin/login','AdminAuth\AuthController@login');
    Route::get('/admin/logout','AdminAuth\AuthController@logout');

    // Registration Routes...
    Route::get('admin/register', 'AdminAuth\AuthController@showRegistrationForm');
    Route::post('admin/register', 'AdminAuth\AuthController@register');

    Route::get('/admin', 'AdminController@index');

});

我创建了一个名为AuthAdmin拉勒维尔的违约AuthController.phpPasswordController.php文件已经存在了。(名称空间相应修改)

首先,在Laravel的文档中,提到了如何在这样的身份验证过程中指定自定义保护,这是不起作用的。

提问于
用户回答回答于

如何在Larvel 5.2中实现多个8月

如上文所述。两桌adminusers

Laravel 5.2有一个新的artisan命令。

php artisan make:auth

它将生成基本的登录/注册。route,viewcontrolleruser桌子。

使出admin表为users为了简单起见。

行政管理人员

app/Http/Controllers/AdminAuth/AuthController

app/Http/Controllers/AdminAuth/PasswordController

(注:我刚从app/Http/Controllers/Auth/AuthController在此)

config/auth.php

//Authenticating guards
'guards' => [
    'user' =>[
        'driver' => 'session',
        'provider' => 'user',
    ],
    'admin' => [
        'driver' => 'session',
        'provider' => 'admin',
    ],
],  

//User Providers
'providers' => [
    'user' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],
    'admin' => [
        'driver' => 'eloquent',
        'model' => App\Admin::class,
    ]
],  

//Resetting Password  
'passwords' => [
    'clients' => [
        'provider' => 'client',
        'email' => 'auth.emails.password',
        'table' => 'password_resets',
        'expire' => 60,
    ],
    'admins' => [
        'provider' => 'admin',
        'email' => 'auth.emails.password',
        'table' => 'password_resets',
        'expire' => 60,
    ],
],  

route.php

Route::group(['middleware' => ['web']], function () {
    //Login Routes...
    Route::get('/admin/login','AdminAuth\AuthController@showLoginForm');
    Route::post('/admin/login','AdminAuth\AuthController@login');
    Route::get('/admin/logout','AdminAuth\AuthController@logout');

    // Registration Routes...
    Route::get('admin/register', 'AdminAuth\AuthController@showRegistrationForm');
    Route::post('admin/register', 'AdminAuth\AuthController@register');

    Route::get('/admin', 'AdminController@index');

});  

AdminAuth/AuthController.php

添加两个方法并指定$redirectTo$guard

protected $redirectTo = '/admin';
protected $guard = 'admin';
public function showLoginForm()
{
    if (view()->exists('auth.authenticate')) {
        return view('auth.authenticate');
    }

    return view('admin.auth.login');
}
public function showRegistrationForm()
{
    return view('admin.auth.register');
}  

它将帮助为管理员打开另一个登录表单。

创建中间件admin

class RedirectIfNotAdmin
{
/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @param  string|null  $guard
 * @return mixed
 */
public function handle($request, Closure $next, $guard = 'admin')
{
    if (!Auth::guard($guard)->check()) {
        return redirect('/');
    }

    return $next($request);
}

}

注册中间件kernel.php

 protected $routeMiddleware = [
    'admin' => \App\Http\Middleware\RedirectIfNotAdmin::class,
];

中使用此中间件AdminController例如,

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class AdminController extends Controller
{
    public function __construct(){
        $this->middleware('admin');
   }
public function index(){
        return view('admin.dashboard');
    }
}

这是所有的需要,使其工作,并获得json的认证管理使用

Auth::guard('admin')->user()

编辑-1

我们可以使用

Auth::user()但是,如果您有两个身份验证表,则必须使用

Auth::guard('guard_name')->user()  

注销

Auth::guard('guard_name')->user()->logout()

对于经过身份验证的用户json

Auth::guard('guard_name')->user()  

用户回答回答于

route.php:

Route::get('/admin', [
  'middleware' => 'admin',
  'uses' => 'AdminController@index'
]);

这是在web中间件组。在此之前我试着把它分开admin中间件组,甚至在auth:admin组,但这不起作用,它只在我将中间件指定为路由本身上的admin时才能工作。

扫码关注云+社区