簡體   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