繁体   English   中英

如何在laravel控制器中调用AdminAuthServiceProvider中的方法?

[英]How to call method from AdminAuthServiceProvider in a laravel controller?

在AuthServiceProvider中,我在启动功能中有以下行。

 Auth::provider('customer', function($app, array $config){
     return new CustomerAuthServiceProvider();
 });

我需要在控制器中调用CustomerAuthServiceProvider中的方法。 我可以做吗 ?

config/App.php的提供者列表中注册您的CustomerAuthServiceProvider ; 然后使用其别名use CustomerAuth (您可以使用任何别名)在控制器顶部的Controller中调用它。 https://laravel.com/docs/5.8/providers

可通过Illuminate\\Auth\\AuthManager Illuminate\\Auth\\CreatesUserProviders特性访问createUserProvider方法

您可以通过Illuminate\\Support\\Facades\\Auth Facade获取AuthManager类的实例。

use Illuminate\Support\Facades\Auth;
//...

class WhatController extends Controller {
   public function index()
   {
      $provider = Auth::createUserProvider('customer');
   }
}

请注意, customer输入必须包含在config/auth.php中的providers数组中

providers' => [
  //...
  customer' => [
   'driver' => 'eloquent',
   'model' => App\WhatCustomer::class,
  ]
]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM