【发布时间】:2017-04-06 22:29:49
【问题描述】:
我有一个在 CakePHP 3 中运行的应用程序,并通过 Composer 安装了我的包。
最近我添加了phpspec/php-diff (https://packagist.org/packages/phpspec/php-diff) 并运行了composer update。正如预期的那样,它已将文件放在vendor/phpspec/php-diff/
我可以像这样在我的 CakePHP 控制器之一中实例化该类:
// src/Controller/UrlsController.php
// ...
use Diff;
public function test()
{
$diff = new Diff(foo, bar);
// This works
}
然而,这个包的文档在 https://github.com/chrisboulton/php-diff/blob/master/example/example.php#L43 上给出了这个例子,它是在 实例化 Diff 之后出现的(正如上面的 test() 所做的那样)。
require_once dirname(__FILE__).'/../lib/Diff/Renderer/Html/Inline.php';
$renderer = new Diff_Renderer_Html_Inline;
echo $diff->render($renderer);
显然 require_once 语句不起作用,因为 Inline.php 不在 Composers 层次结构中的位置。
那么你如何实例化new Diff_Renderer_Html_Inline?如果我只是这样使用它,它会出错
找不到类“App\Controller\Diff_Renderer_Html_Inline”
我认为另一种解决方法可能是更改require_once 中的路径,使其指向它在vendor/ 中的位置。但肯定有更好的解决方案吗?
【问题讨论】:
-
这是 PSR-0 风格的自动加载(作曲家支持)所以
new \Diff\Renderer\Html\Inline可能会工作(也许?) -
谢谢,我已经尝试过了,但它给出了一个类似的错误,
Class 'Diff\Renderer\Html\Inline' not found -
如果没有,那么
new \Diff_Renderer_Html_Inline(根 NS 相对)可能会起作用 -
啊!是的,这确实有效:) 如果您将其作为答案发布,我会接受。非常感谢。
标签: php cakephp composer-php