【问题标题】:How can I fix the namespace error when using classes within the app folder?在 app 文件夹中使用类时如何修复命名空间错误?
【发布时间】:2023-02-02 04:03:10
【问题描述】:

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

以前的结构

应用程序 -->服务 ------>条纹

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

  1. 条纹.php
  2. StripeFacade.php
  3. 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
    

    我尝试将服务目录添加到我的 psr-4 中,但似乎没有任何成功,即使在转储配置和自动加载之后也是如此。

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

    有帮助吗? :)

【问题讨论】:

  • 尝试命名空间 App 而不是 app
  • @RonvanderHeijden 早些时候尝试过。又做了一遍,现在报错Class "App\Services\Stripe\StripeServiceProvider" not found
  • 更改名称后,在 CLI 中运行 composer du
  • 在 CLI 中运行 composer dump-autoload
  • @josezenem 控制台中的警告是Class App\Services\Stripe\StripeServiceProvider located in ./app/Services /Stripe/StripeServiceProvider.php does not comply with psr-4 autoloading standard. Skipping.其他 Stripe 文件收到类似的警告。

标签: laravel laravel-8 facade


【解决方案1】:

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

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

我注意到./app/Services /Stripe 中多了一个空格。

也许您已经创建了 Services / 目录,并且末尾有一个空格。

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

【讨论】:

  • 你是对的!重要的捂脸时刻
【解决方案2】:

使用下面的代码

 namespace AppServicesStripe;

 use AppServicesStripeStripeServiceProvider::class

【讨论】:

  • 我到底应该在哪里使用它?
猜你喜欢
  • 1970-01-01
  • 2015-12-10
  • 1970-01-01
  • 1970-01-01
  • 2014-11-14
  • 1970-01-01
  • 2021-03-27
  • 2019-05-23
  • 1970-01-01
相关资源
最近更新 更多