经过大量的挖掘和大量的问题与答案之后,我终于设法使用带有两个表的Laravel 5.2 Multi Auth,所以我正在编写自己的问题的Answer。
如何在Larvel 5.2中实现Multi Auth
正如刚才提到的。两个表admin
和users
Laravel 5.2有一个新artisan
命令。
php artisan make:auth
它将生成基本的登录/注册route
,view
并controller
用于user
表。
为简单起见,将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()
编辑2
现在您可以下载Laravel 5.2 Multiauth实现的项目http://imrealashu.in/code/laravel/multi-auth-with-laravel-5-2-2/
protected $guard = 'guard_name'
现在可以使用。