在 Laravel-4 中加载外部库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19754431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:37:21  来源:igfitidea点击:

Loading external library in Laravel-4

phplaravellaravel-4

提问by Patrick Reck

I am trying to load the Simple HTML DOM Parserin my Laravel-4project. However, I can't get it to find the class. I am getting this error:

我正在尝试在我的项目中加载Simple HTML DOM ParserLaravel-4。但是,我无法找到课程。我收到此错误:

Class 'Libraries\SimpleHtmlDom\simple_html_dom_node' not found

I've put simple_html_dom.phpin app\librariesand

我已经把simple_html_dom.phpapp\libraries

namespace Libraries\SimpleHtmlDom;

in the top of simple_html_dom.php.

simple_html_dom.php.

In global.phpI've added the librariesfolder:

global.php我添加了libraries文件夹:

ClassLoader::addDirectories(array(

    app_path().'/commands',
    app_path().'/controllers',
    app_path().'/models',
    app_path().'/database/seeds',
    app_path().'/libraries',

));

And finally, in my controller, I am trying to instantiate it:

最后,在我的控制器中,我试图实例化它:

$parser = new Libraries\SimpleHtmlDom\simple_html_dom_node;

What am I doing wrong?

我究竟做错了什么?

回答by fideloper

Specific to your use case

特定于您的用例

To (hopefully) solve your problem without changing your current structure, you may just need to run:

要(希望)在不更改当前结构的情况下解决您的问题,您可能只需要运行:

$ composer dump-autoload

Or you may need to add an autoloaded section in composer.jsonalong with the others:

或者您可能需要composer.json与其他部分一起添加一个自动加载的部分:

    "autoload": {
            "classmap": [
                    "app/commands",
                    "app/controllers",
                    "app/models",
                    "app/database/migrations",
                    "app/database/seeds",
                    "app/tests/TestCase.php",
                    "app/libraries"
            ]
    },

And then run composer dump-autoloadagain.

然后composer dump-autoload再次运行。

Packagist (better?) Solution

Packagist(更好?)解决方案

It looks like there's a Composer package you could be using instead: https://packagist.org/packages/sunra/php-simple-html-dom-parser

看起来您可以使用 Composer 包:https: //packagist.org/packages/sunra/php-simple-html-dom-parser

This makes adding it to your project easier:

这使它更容易添加到您的项目中:

A.Add dependency to composer.json(either via this CLI call or by adding manually to your composer.json file):

A.添加依赖项composer.json(通过此 CLI 调用或手动添加到您的 composer.json 文件):

# Run this command:
$ composer require "sunra/php-simple-html-dom-parser": "1.5.0.*@dev"

B.Use new packagein your code

B.在你的代码中使用新的包

$dom = HtmlDomParser::file_get_html( $file_name );