Add external libraries to Symfony2 project

北慕城南 提交于 2019-12-04 12:18:57

问题


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!


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/8789616/add-external-libraries-to-symfony2-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!