使用 Composer 自动加载 PSR-4 [英] PSR-4 autoloading with Composer

查看:41
本文介绍了使用 Composer 自动加载 PSR-4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Composer 的自动加载类系统运行了一个传送门:

I run a portail with composer's autoloading class system:

"autoload": {
    "psr-4": {
        "Portal\": "src/"
    }
}

它在我运行 composer.phar dump -o 时有效,例如我的类 Boostrap 被很好地引用到 vendor/composer/autoload_classmap.php 文件中:

It works when I run composer.phar dump -o, for instance my class Boostrap is well referenced into vendor/composer/autoload_classmap.php file:

'Portal\Core\Bootstrap' => $baseDir . '/src/core/Bootstrap.php',

但是当我不在自动加载转储上运行优化选项时,自动加载系统不再工作:

But when I don't run the optimized option on autoload dumping, the autoloading system doesn't works anymore:

Fatal error: Class 'PortalCoreBootstrap' not found in /var/www/portail/prod/web/index.php on line 7

如何在没有 -o 选项的情况下使自动加载工作?

How can I make autoloading works without -o option?

推荐答案

有两种方法可以解决.

  1. composer.json 改为

"Portal\Core\": "src/core/"

  • 或者将core目录重命名为Core

    https://getcomposer.org/doc/04-schema.md#psr-4

    子目录名称必须与子命名空间名称的大小写匹配.

    The subdirectory name MUST match the case of the sub-namespace names.

    http://www.php-fig.org/psr/psr-4/

    这篇关于使用 Composer 自动加载 PSR-4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆