php – Symfony4加载类自定义文件夹时出错“预计会找到类…但是找不到它”

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – Symfony4加载类自定义文件夹时出错“预计会找到类…但是找不到它”脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
问题

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

所以我添加一个自定义服务命名空间
services.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文件夹中的控制器似乎加载正常.
在这做错了什么?

重现步骤

我创建了一个演示仓库以隔离问题.

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加载类自定义文件夹时出错“预计会找到类…但是找不到它”全部内容,希望文章能够帮你解决php – Symfony4加载类自定义文件夹时出错“预计会找到类…但是找不到它”所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。