在应用程序文件夹中使用类时如何修复命名空间错误?

发布于 2025-01-12 13:27:39 字数 1843 浏览 3 评论 0原文

我正在将旧版 Laravel 应用程序从 Laravel 5 升级到 8,但遇到了麻烦。我的服务提供商都不起作用,我不明白为什么。

上一个结构

应用程序 -->服务 ------>Stripe

在每个服务提供者文件夹中,我将创建三个文件,如下所示:

  1. Stripe.php
  2. StripeFacade.php
  3. StripeServiceProvider.php

stripe.php

<?php

namespace app\Services\Stripe;


class Stripe
{

}

?>

StripeFacade。 ,

<?php

namespace app\Services\Stripe;

use Illuminate\Support\Facades\Facade;

class StripeFacade extends Facade
{
    protected static function getFacadeAccessor()
    {
        return 'Stripe';
    }
}

在我的 Config/app.php 文件中的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();
        });
    }
}

我会像这样注册服务提供者和外观:

'providers' => [
    app\Services\Stripe\StripeServiceProvider::class,
],

'aliases' => [
    'Stripe' => app\Services\Stripe\StripeFacade::class,
]

在我的控制器中,我' d 打电话给条纹然后

use Stripe;

...

public function example(){
   $auth = Stripe::auth();
}

我会在 Config/app.php 文件中收到此错误

Class "app\Services\Stripe\StripeServiceProvider" not found

我尝试将 Services 目录添加到我的 psr-4 中,但似乎没有任何运气,即使在转储配置和自动加载。

"autoload": {
        "psr-4": {
            "App\\": "app/",
            "Services\\": "app/Services",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        }
    },

有什么帮助吗? :)

I am upgrading a legacy laravel application from Laravel 5 to 8 and ran into a brick wall. None of my service providers work, and I cannot figure out why.

Previous Structure

app
-->Services
------>Stripe

Within each service provider folder, I'd create three files like so:

  1. Stripe.php
  2. StripeFacade.php
  3. StripeServiceProvider.php

within stripe.php

<?php

namespace app\Services\Stripe;


class Stripe
{

}

?>

within StripeFacade.php

<?php

namespace app\Services\Stripe;

use Illuminate\Support\Facades\Facade;

class StripeFacade extends Facade
{
    protected static function getFacadeAccessor()
    {
        return 'Stripe';
    }
}

within 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();
        });
    }
}

in my Config/app.php file, I'd register the service provider and facade like so:

'providers' => [
    app\Services\Stripe\StripeServiceProvider::class,
],

'aliases' => [
    'Stripe' => app\Services\Stripe\StripeFacade::class,
]

In my controller, I'd call the Stripe service as

use Stripe;

...

public function example(){
   $auth = Stripe::auth();
}

Then I'd get this error in the Config/app.php file

Class "app\Services\Stripe\StripeServiceProvider" not found

I tried adding the Services directory to my psr-4 and didn't seem to get any luck, even after dumping configs and autoload.

"autoload": {
        "psr-4": {
            "App\\": "app/",
            "Services\\": "app/Services",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        }
    },

any help? :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏了南城 2025-01-19 13:27:39

在您的评论中,您发布了一个错误:

类 App\Services\Stripe\StripeServiceProvider 位于 ./app/Services /Stripe/StripeServiceProvider.php 不符合 psr-4 自动加载标准。跳过。

我注意到 ./app/Services /Stripe 中有一个额外的空格。

也许您创建的 Services/ 目录末尾有一个空格。

一些改进是将 app\ 重命名为 App\ 并从 composer.json< 中删除 "Services\\": 行/代码>。在这些更改之后运行composer dump-autoload

In your comment, you posted an error:

Class App\Services\Stripe\StripeServiceProvider located in ./app/Services /Stripe/StripeServiceProvider.php does not comply with psr-4 autoloading standard. Skipping.

I noticed an extra space in ./app/Services /Stripe.

Perhaps you have created the Services / directory with a space at the end.

Some improvements are to rename app\ to App\ and remove the "Services\\": line from your composer.json. Run composer dump-autoload after these changes.

耀眼的星火 2025-01-19 13:27:39

使用下面的代码

 namespace App\Services\Stripe;

 use App\Services\Stripe\StripeServiceProvider::class

Use the below code

 namespace App\Services\Stripe;

 use App\Services\Stripe\StripeServiceProvider::class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文