gpt4 book ai didi

php - Symfony4 错误加载类自定义文件夹 "Expected to find class... but it was not found"

转载 作者:可可西里 更新时间:2023-10-31 23:05:26 24 4
gpt4 key购买 nike

问题

我正在尝试设置自定义目录结构对于我的 Symfony 项目中的一些共享类。我想在我的根目录中创建一个自定义文件夹项目,我想使用 Symfony 自动加载自动注册服务的功能那个文件夹。

所以我向服务.yaml 文件:

# src ./config/services.yaml
services:
...

TestNamespace\:
resource: '../TestNamespace/*'

...

然后我在自定义文件夹中添加了一个空类:

# src ./TestNamespace/TestClass.php

namespace TestNamespace;

class TestClass
{

}

当我运行该应用程序时,出现以下错误:

(1/2) InvalidArgumentException
Expected to find class "TestNamespace\TestClass" in file
"/path/to/ClassLoadErrorDemo/demo/TestNamespace/TestClass.php"
while importing services from resource
"../TestNamespace/*", but it was not found! Check the
namespace prefix used with the resource.

(2/2) FileLoaderLoadException
Expected to find class "TestNamespace\TestClass" in file
"/path/to/ClassLoadErrorDemo/demo/TestNamespace/TestClass.php" while
importing services from resource "../TestNamespace/*", but it was not
found! Check the namespace prefix used with the resource in
/path/to/ClassLoadErrorDemo/demo/config/services.yaml (which is loaded
in resource "/path/to/ClassLoadErrorDemo/demo/config/services.yaml").

我仔细检查了路径、命名空间和类多次命名,一切似乎都很好,我不明白为什么我仍然收到错误。./src 文件夹中的 Controller 似乎加载正常。我在这里做错了什么?

重现步骤

我创建了一个演示存储库来隔离问题。

git clone https://github.com/smoelker/SymfonyClassLoadErrorDemo.git
cd SymfonyClassLoadErrorDemo/demo
composer install
mv TestNamespace/TestClass.php_ TestNamespace/TestClass.php
php bin/console server:start

最佳答案

更新您的 composer.json 自动加载设置

{
[...]
"autoload": {
"psr-4": {
"TestNamespace\\": "TestNamespace/",
"": "src/"
}
},
[...]
}

运行后:composer dump-autoload 并重试。

关于php - Symfony4 错误加载类自定义文件夹 "Expected to find class... but it was not found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47954510/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com