我正在將一個遺留的 laravel 應用程式從 Laravel 5 升級到 8 并遇到了一堵磚墻。我的服務提供商都沒有作業,我不知道為什么。
以前的結構
應用程式 --> 服務 ------> 條紋
在每個服務提供商檔案夾中,我將創建三個檔案,如下所示:
- 條紋.php
- StripeFacade.php
- StripeServiceProvider.php
之內stripe.php
<?php
namespace app\Services\Stripe;
class Stripe
{
}
?>
之內StripeFacade.php
<?php
namespace app\Services\Stripe;
use Illuminate\Support\Facades\Facade;
class StripeFacade extends Facade
{
protected static function getFacadeAccessor()
{
return 'Stripe';
}
}
之內StripeServiceProvider.php
<?php
namespace app\Services\Stripe;
use Illuminate\Support\ServiceProvider;
class StripeServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton('Stripe', function($app) {
return new Stripe();
});
}
}
在我的Config/app.php檔案中,我會像這樣注冊服務提供者和外觀:
'providers' => [
app\Services\Stripe\StripeServiceProvider::class,
],
'aliases' => [
'Stripe' => app\Services\Stripe\StripeFacade::class,
]
在我的控制器中,我將 Stripe 服務稱為
use Stripe;
...
public function example(){
$auth = Stripe::auth();
}
然后我會在Config/app.php檔案中得到這個錯誤
Class "app\Services\Stripe\StripeServiceProvider" not found
我嘗試將 Services 目錄添加到我的 psr-4 中,即使在轉儲 congif 和自動加載之后似乎也沒有任何運氣。
"autoload": {
"psr-4": {
"App\\": "app/",
"Services\\": "app/Services",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
有什么幫助嗎?:)
uj5u.com熱心網友回復:
在您的評論中,您發布了一個錯誤:
位于 ./app/Services /Stripe/StripeServiceProvider.php 的類 App\Services\Stripe\StripeServiceProvider 不符合 psr-4 自動加載標準。跳過。
我注意到./app/Services /Stripe.
也許您創建的Services /目錄末尾有一個空格。
一些改進是重命名app\并App\從"Services\\":您的composer.json. composer dump-autoload在這些更改之后運行。
uj5u.com熱心網友回復:
使用下面的代碼
namespace App\Services\Stripe;
use App\Services\Stripe\StripeServiceProvider::class
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/440031.html
上一篇:兩步授權中間件
下一篇:使用App::environment()與app()->environment()與config('app.env')之間的區別
