将外部库添加到 Symfony2 项目

发布于 2024-12-26 06:06:18 字数 1206 浏览 1 评论 0原文

我正在尝试添加外部库(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东京女 2025-01-02 06:06:18

您正在尝试注册名称空间,但您的类没有名称空间。尝试向其中添加名称空间或使用 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.

憧憬巴黎街头的黎明 2025-01-02 06:06:18

我是 Symfony2 的新手,但正如我所见,您不尊重自动加载器的 PSR。

我想您应该这样做:

# /vendor/phpsimpledom/lib/Phpsimpledom/Phpsimpledom.php

require_once __DIR__.'/src/simple_html_dom.php';

class Phpsimpledom_Phpsimpledom extends simple_html_dom_node {

}

请注意,正确的文件名是 /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:

# /vendor/phpsimpledom/lib/Phpsimpledom/Phpsimpledom.php

require_once __DIR__.'/src/simple_html_dom.php';

class Phpsimpledom_Phpsimpledom extends simple_html_dom_node {

}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文