将外部库添加到 Symfony2 项目
我正在尝试添加外部库(PHP Simple DOM Parser,http://simplehtmldom.sourceforge.net/index .htm)到 Symfony2 项目。我学习了一个教程,解释如何将第三方库包含到 Symfony2 http://www.kiwwito.com/article/add-third-party-libraries-to-symfony-2。
我设置了一个类文件,例如:
# vendor/phpsimpledom/lib/Phpsimpledom/simple_html_dom.php
require_once __DIR__.'/src/simple_html_dom.php';
class Phpsimpledom_Phpsimpledom extends simple_html_dom_node {
}
并在我的自动加载器(autoload.php)中注册了我的类:
$loader->registerNamespaces(array(
...
'Phpsimpledom' => __DIR__.'/../vendor/phpsimpledom/lib/',
...
),));
我正在尝试调用:
$phpsimpledom = new \Phpsimpledom();
但这给我带来了一个错误(致命错误:未找到类“simple_html_dom_node”)。
但是: 库的主文件 (simple_html_dom.php) 由不属于类的函数组成。
当我尝试直接使用该文件时,它也不起作用:
$loader->registerNamespaces(array(
...
'Phpsimpledom' => __DIR__.'/../vendor/phpsimpledom/lib/Phpsimpledom/src/simple_html_dom.php',
...
),));
有任何提示吗?
谢谢!
I am trying to add an external library (PHP Simple DOM Parser, http://simplehtmldom.sourceforge.net/index.htm) to a Symfony2 project. I took a tutorial that explains how to include third party libraries to Symfony2 http://www.kiwwito.com/article/add-third-party-libraries-to-symfony-2.
I set up a class file like:
# vendor/phpsimpledom/lib/Phpsimpledom/simple_html_dom.php
require_once __DIR__.'/src/simple_html_dom.php';
class Phpsimpledom_Phpsimpledom extends simple_html_dom_node {
}
and registered my class in my Autoloader (autoload.php):
$loader->registerNamespaces(array(
...
'Phpsimpledom' => __DIR__.'/../vendor/phpsimpledom/lib/',
...
),));
I am trying to call:
$phpsimpledom = new \Phpsimpledom();
but this throughs me an error (Fatal error: Class 'simple_html_dom_node' not found).
However: The main file of the library (simple_html_dom.php) consists of functions that do not belong to a class.
When I try to use the file directly, it also doesn't work:
$loader->registerNamespaces(array(
...
'Phpsimpledom' => __DIR__.'/../vendor/phpsimpledom/lib/Phpsimpledom/src/simple_html_dom.php',
...
),));
Any hints?
THANKS!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在尝试注册名称空间,但您的类没有名称空间。尝试向其中添加名称空间或使用 RegisterPrefixes()。
顺便说一句:你知道 Symfony 组件之一基本上与 php simpledom 做同样的事情吗?它称为 DomCrawler,支持 xpath 和 CSS 选择器。
You're trying to register a namespace but your class has no namespace. Try adding a namespace to it or use RegisterPrefixes().
BTW: did you know that one of the Symfony components is basically doing the same thing as php simpledom? It's called DomCrawler and it has a support for both xpath and CSS selectors.
我是 Symfony2 的新手,但正如我所见,您不尊重自动加载器的 PSR。
我想您应该这样做:
请注意,正确的文件名是
/vendor/phpsimpledom/lib/Phpsimpledom/Phpsimpledom.php
因为调用必须包含要工作的命名空间。希望现在有效。
I'm new to Symfony2 but as i can see, you are not respecting the PSR for autoloader.
I'm presumable thinking you should do:
Note that the correct filename would be
/vendor/phpsimpledom/lib/Phpsimpledom/Phpsimpledom.php
as the call must include the namespace to work.Hope it works now.