问题描述
我试图实施 laravel 5.2多认证
。我下面
我的 Auth.php
<?php
return [
'multi' => array(
'user' => array(
'driver' => 'eloquent',
'model' => 'App\User',
'table' => 'users',
),
'admin' => array(
'driver' => 'database',
'model' => 'App\Admin',
'table' => 'tbl_admin_user',
)
),
'password' => [
'email' => 'emails.password',
'table' => 'password_resets',
'expire' => 60,
],
];
我的 App.php
'providers' => [
/*
* Laravel Framework Service Providers...
*/
//Illuminate\Auth\AuthServiceProvider::class,
Ollieread\Multiauth\MultiauthServiceProvider::class,
Illuminate\Broadcasting\BroadcastServiceProvider::class,
Illuminate\Bus\BusServiceProvider::class,
Illuminate\Cache\CacheServiceProvider::class,
Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
Illuminate\Cookie\CookieServiceProvider::class,
Illuminate\Database\DatabaseServiceProvider::class,
Illuminate\Encryption\EncryptionServiceProvider::class,
Illuminate\Filesystem\FilesystemServiceProvider::class,
Illuminate\Foundation\Providers\FoundationServiceProvider::class,
Illuminate\Hashing\HashServiceProvider::class,
Illuminate\Mail\MailServiceProvider::class,
Illuminate\Pagination\PaginationServiceProvider::class,
Illuminate\Pipeline\PipelineServiceProvider::class,
Illuminate\Queue\QueueServiceProvider::class,
Illuminate\Redis\RedisServiceProvider::class,
Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
Illuminate\Session\SessionServiceProvider::class,
Illuminate\Translation\TranslationServiceProvider::class,
Illuminate\Validation\ValidationServiceProvider::class,
Illuminate\View\ViewServiceProvider::class,
/*
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
],
/*
|--------------------------------------------------------------------------
| Class Aliases
|--------------------------------------------------------------------------
|
| This array of class aliases will be registered when this application
| is started. However, feel free to register as many as you wish as
| the aliases are "lazy" loaded so they don't hinder performance.
|
*/
'aliases' => [
'App' => Illuminate\Support\Facades\App::class,
'Artisan' => Illuminate\Support\Facades\Artisan::class,
'Auth' => Illuminate\Support\Facades\Auth::class,
'Blade' => Illuminate\Support\Facades\Blade::class,
'Cache' => Illuminate\Support\Facades\Cache::class,
'Config' => Illuminate\Support\Facades\Config::class,
'Cookie' => Illuminate\Support\Facades\Cookie::class,
'Crypt' => Illuminate\Support\Facades\Crypt::class,
'DB' => Illuminate\Support\Facades\DB::class,
'Eloquent' => Illuminate\Database\Eloquent\Model::class,
'Event' => Illuminate\Support\Facades\Event::class,
'File' => Illuminate\Support\Facades\File::class,
'Gate' => Illuminate\Support\Facades\Gate::class,
'Hash' => Illuminate\Support\Facades\Hash::class,
'Lang' => Illuminate\Support\Facades\Lang::class,
'Log' => Illuminate\Support\Facades\Log::class,
'Mail' => Illuminate\Support\Facades\Mail::class,
'Password' => Illuminate\Support\Facades\Password::class,
'Queue' => Illuminate\Support\Facades\Queue::class,
'Redirect' => Illuminate\Support\Facades\Redirect::class,
'Redis' => Illuminate\Support\Facades\Redis::class,
'Request' => Illuminate\Support\Facades\Request::class,
'Response' => Illuminate\Support\Facades\Response::class,
'Route' => Illuminate\Support\Facades\Route::class,
'Schema' => Illuminate\Support\Facades\Schema::class,
'Session' => Illuminate\Support\Facades\Session::class,
'Storage' => Illuminate\Support\Facades\Storage::class,
'URL' => Illuminate\Support\Facades\URL::class,
'Validator' => Illuminate\Support\Facades\Validator::class,
'View' => Illuminate\Support\Facades\View::class,
],
];
当我跑这个项目,我看到的错误
When I ran the project, I saw the error
调用未定义的方法照亮\\基金会\\应用:: bindShared()
在MultiauthServiceProvider.php线13
我改变了 MultiAuthServiceProvider.php
到
$this->app->singleton('auth', function ($app) {
$app['auth.loaded'] = true;
return new \Ollieread\Multiauth\MultiManager($app);
});
和如附件截图现在我看到这个错误
我不知道什么是试图实例化门门面
and now I am seeing this error as attached in the screenshotI am not sure what is trying to instantiate the Gate Facade
推荐答案
而不是创建一个自定义多AUTH这样的,我建议你使用Laravel定制AUTH 后卫
和AUTH 提供商
。老实说第二种方法还没有测试,但我认为它应该工作。
Instead of creating a custom multi auth like that, I recommend you to use Laravel custom auth guard
and auth provider
. Honestly the second method is not tested yet but I think it should be worked.
1。一个模型中的多个守卫
在你的配置/ auth.php
添加一个新的组织后卫:
On your config/auth.php
add a new guard:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
// new guard for admin using the same model and table.
'admin' => [
'driver' => 'session',
'provider' => 'users',
],
],
然后使用你的组路线 AUTH:管理员
,当然您还可以创建另一个中间件,可以检查当前用户是否为管理员用户。要获得认证的用户实例,你需要指定自定义后卫:验证::后卫(管理员) - GT;用户()
Then group your routes using auth:admin
, of course you also have to create another middleware that can check whether current user is an admin user. To get authenticated user instance you need to specify the custom guard: Auth::guard('admin')->user()
.
要确保它的工作,创建两路一组与 AUTH
中间件和一个与 AUTH:管理员
中间件,使用当你登录的用户的路线 AUTH:系统管理员
试图访问你应该被要求重新登录等航线组
To make sure it's worked, create two routes group one with auth
middleware and one with auth:admin
middleware, when you have logged in user on routes that uses auth:admin
try to access the other routes group you should be asked to login again.
Route::group(['middleware' => 'auth'], function () {
Route::get('/', function () {
return 'You are logged in using guard: web';
});
});
Route::group(['middleware' => 'auth:admin'], function () {
Route::get('/admin', function () {
return 'You are logged in using guard: admin';
});
});
2。多个模型
再次在配置/ auth.php
,创建新的自定义服务提供商:
Again on your config/auth.php
, create new custom provider:
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class',
],
],
然后创建管理员自定义的后卫
为供应商:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
// new guard for admin using custom provider.
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
],
使用这种方法不需要创建自定义的中间件,可以使用 AUTH:系统管理员
要细
这篇关于Laravel- Multiauth不Laravel 5.2工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!