【问题标题】:Creating a new project and bundle - "Did you forget a 'use' statement for another namespace?"创建一个新项目和捆绑包 - “您是否忘记了另一个命名空间的 'use' 语句?”
【发布时间】:2021-09-05 01:12:44
【问题描述】:

我正在尝试使用测试包创建一个测试项目。我收到以下错误:

尝试从命名空间“test”加载类“TestBundle”。您是否忘记了另一个命名空间的“使用”语句?

我已阅读 Symfony 网站上的所有说明并尝试了许多不同的方法,但没有任何乐趣。

test/config/bundles.php

    return [
        Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
        test\TestBundle::class => ['all' => true],
    ];

test/src/TestBundle/TestBundle.php

namespace test\TestBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class TestBundle extends Bundle
{
    public function getPath(): string
    {
        return \dirname(__DIR__);
    }
}

test/src/TestBundle/composer.json

{
    "type": "symfony-bundle",
    "name": "TestBundle",
    "type": "testing building a reusuable bundle",
    "license": "proprietary",
    "minimum-stability": "dev",
    "prefer-stable": true,
    "require": {
        "php": ">=7.2.5",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "symfony/console": "5.4.*",
        "symfony/dotenv": "5.4.*",
        "symfony/flex": "^1.3.1",
        "symfony/framework-bundle": "5.4.*",
        "symfony/runtime": "5.4.*",
        "symfony/yaml": "5.4.*"
    },
    "require-dev": {
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": {
            "*": "dist"
        },
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "test\\TestBundle\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "test\\TestBundle\\Tests\\": "tests/"
        }
    },
    "replace": {
        "symfony/polyfill-ctype": "*",
        "symfony/polyfill-iconv": "*",
        "symfony/polyfill-php72": "*"
    },
    
    "conflict": {
        "symfony/symfony": "*"
    },
    "extra": {
        "symfony": {
            "allow-contrib": false,
            "require": "5.4.*"
        }
    }
}

test/composer.json

{
    "type": "project",
    "license": "proprietary",
    "minimum-stability": "dev",
    "prefer-stable": true,
    "require": {
        "php": ">=7.2.5",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "symfony/console": "5.4.*",
        "symfony/dotenv": "5.4.*",
        "symfony/flex": "^1.3.1",
        "symfony/framework-bundle": "5.4.*",
        "symfony/runtime": "5.4.*",
        "symfony/yaml": "5.4.*",
    "symfony/yaml": "5.4.*",   
    "test/TestBundle": "*"
    },
    "require-dev": {
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": {
            "*": "dist"
        },
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\\Tests\\": "tests/"
        }
    },
    "replace": {
        "symfony/polyfill-ctype": "*",
        "symfony/polyfill-iconv": "*",
        "symfony/polyfill-php72": "*"
    },
    "scripts": {
        "auto-scripts": {
            "cache:clear": "symfony-cmd",
            "assets:install %PUBLIC_DIR%": "symfony-cmd"
        },
        "post-install-cmd": [
            "@auto-scripts"
        ],
        "post-update-cmd": [
            "@auto-scripts"
        ]
    },
    "conflict": {
        "symfony/symfony": "*"
    },
    "extra": {
        "symfony": {
            "allow-contrib": false,
            "require": "5.4.*"
        }
    }
}

【问题讨论】:

  • 不,我还没到那一步,没有依赖关系,它现在基本上是一个空包,所以它应该不需要composer来安装它吗?
  • 按照我认为 bundles.php 的说明操作?
  • 不,这无关。 bundles.php 怎么能告诉自动加载器任何事情,而文件不知道所引用的类在哪里定义。
  • 啊,它是 composer.json 文件吗?我忘记在帖子中添加了,我现在就添加它。
  • 请分享更多细节。据我所知,您没有任何名为 test\TestBundle 的类,而是一个名为 test\TestBundle\TestBundle 的类 - 您是否尝试在 bundles.php 中使用它?

标签: php symfony composer-php


【解决方案1】:

由于您没有使用 composer 安装“bundle”,因此不会生成自动加载器,并且永远不会找到与您的新包相关的类。

bundle 的composer.json 文件是无关紧要的,因为你没有通过composer 安装包。因此,它永远不会被读取。

但您可以指示 composer 生成自动加载器文件,将这个新包考虑在内。

假设这是你的目录结构:

- config     <--- where bundles.php resides, among other files
- src        <--- the application code that "consumes" your bundle,  
- testBundle <--- This is where you have your bundle's code. 
- vendor     
- etc

请注意,您的包的代码与应用程序代码的其余部分不在同一目录中

现在在您的 应用程序 composer.json 文件中,您应该需要添加如下内容:

{
    "autoload": {
        "psr-4": 
            {
            "App\\": "src/",
            "test\\TestBundle\\": "testBundle/"
        }
    }
}

完成此操作后,您应该重新转储自动加载器 (composer dump-autoload),文件应该可以被发现。

(请注意,在您的问题中,您说TestBundle 的命名空间是test\TestBundle,但是在bundles.php 中您尝试使用test\TestBundle::class。其中一个是错误的,它要么是namespace test; , 或test\TestBundle\TestBundle::class)。

【讨论】:

  • @LeeTee 这是一个简单的工作example
猜你喜欢
  • 2019-03-23
  • 2016-11-07
  • 2017-02-12
  • 2015-11-07
  • 2019-10-09
  • 2019-11-18
  • 2020-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多