简体   繁体   English

加载未在vendor / composer / autoload_namespaces.php中列出的Composer软件包

[英]Loading a Composer package not listed in vendor/composer/autoload_namespaces.php

I have an application written in CakePHP 3. I've installed Cake and other packages via Composer. 我有一个用CakePHP 3编写的应用程序。我已经通过Composer安装了Cake和其他软件包。

The most recent one I installed was fineuploader/php-traditional-server ( https://packagist.org/packages/fineuploader/php-traditional-server ) by placing this in my composer.json then running composer update : 我安装的最新版本是fineuploader/php-traditional-serverhttps://packagist.org/packages/fineuploader/php-traditional-server ),方法是将其放置在composer.json然后运行composer update

 "require": {
    "fineuploader/php-traditional-server": "1.1.0"
 }

This has downloaded the files as expected into vendor/fineuploader/php-traditional-server . 这已按预期将文件下载到vendor/fineuploader/php-traditional-server

However, if I try to reference that in one of my Cake Controllers, like this: 但是,如果我尝试在我的一个Cake Controller中引用它,如下所示:

use UploadHandler;

public function test()
{
    var_dump(new \UploadHandler);
}

It errors saying Class 'UploadHandler' not found 错误提示未找到类“ UploadHandler”

The only way I've been able to get the above to run is by hard-coding a link to the appropriate file. 我能够执行以上操作的唯一方法是通过硬编码到相应文件的链接。 So instead of use UploadHandler; 因此,不要use UploadHandler; I have 我有

require_once(ROOT . DS . 'vendor/fineuploader/php-traditional-server/handler.php'); 

I read on https://jtreminio.com/2012/10/composer-namespaces-in-5-minutes/ that vendor/composer/autoload_namespaces.php is supposed to contain the namespaces that can be autoloaded. 我在https://jtreminio.com/2012/10/composer-namespaces-in-5-minutes/上阅读, vendor/composer/autoload_namespaces.php应该包含可以自动加载的名称空间。 But when I open that file up, there's no reference to this package (in fact there are only about 3 whereas my vendor directory has loads of packages some of which I can use just fine). 但是,当我打开该文件时,并没有对该程序包的引用(实际上只有大约3个,而我的vendor目录中有很多程序包,我可以很好地使用其中的一些程序包)。

So what is the correct way to load UploadHandler ? 那么加载UploadHandler的正确方法是什么? I thought the point of using Composer was that it took care of autoloading these files, so you didn't have to use things like require() ? 我认为使用Composer的要点是它会自动加载这些文件,因此您不必使用require()类的东西?

Have you read the note in the handler.php file? 您是否已阅读handler.php文件中的注释?

Do not use or reference this directly from your client-side code. 不要直接从客户端代码中使用或引用它。 Instead, this should be required via the endpoint.php or endpoint-cors.php file(s) 相反,这应该通过endpoint.php或endpoint-cors.php文件来要求

So AFAICT the package doesn't define any autoloading capabilites because you're not supposed to use the UploadHandler class directly, but instead you should require the endpoint.php or endpoint-cors.php file, which do not include any classes, and should only be required when needed. 因此,AFAICT程序包未定义任何自动加载功能,因为您不应直接使用UploadHandler类,而应使用不包含任何类的endpoint.phpendpoint-cors.php文件,并且应仅在需要时才需要。

Surely these endpoint files are not compatible with CakePHP, so you may have a reason to use the handler class directly. 当然,这些端点文件与CakePHP不兼容,因此您可能有理由直接使用处理程序类。 The package however doesn't use namespaces, and the classname doesn't match the filename, ie it follows neither PSR-0 nor PSR-4 conventions, and as already mentioned, the packages composer.json doesn't define any autoloading configuration, so no matter what conventions the package would follow, this would result in no autoloading capabilities for the packages PHP classes/files. 但是,该软件包不使用名称空间,并且类名与文件名不匹配,即它既不遵循PSR-0也不遵循PSR-4约定,并且如上所述,软件包composer.json没有定义任何自动加载配置,因此,无论软件包遵循什么约定,都会导致PHP类/文件没有自动加载功能。

So if you want autoloading capability, you could fix this on your end by for example adding a classmap autoload entry to your projects composer.json , like: 因此,如果您想要自动加载功能,则可以通过例如在项目composer.json添加类classmap自动加载项来最终解决此问题,例如:

{
    "autoload": {
        "classmap": [
            "vendor/fineuploader/php-traditional-server/"
        ]
    }
}

Then re-dump the autoloader, and autoloading should work fine: 然后重新转储自动装带器,自动装带应能正常工作:

$ composer dump-autoload

See also 也可以看看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM