Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading external library in Laravel-4

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

Class 'Libraries\SimpleHtmlDom\simple_html_dom_node' not found

I've put simple_html_dom.php in app\libraries and

namespace Libraries\SimpleHtmlDom;

in the top of simple_html_dom.php.

In global.php I've added the libraries folder:

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?

like image 413
Patrick Reck Avatar asked Nov 03 '13 15:11

Patrick Reck


1 Answers

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.json along with the others:

    "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-autoload again.

Packagist (better?) Solution

It looks like there's a Composer package you could be using instead: 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):

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

B. Use new package in your code

$dom = HtmlDomParser::file_get_html( $file_name );
like image 142
fideloper Avatar answered Oct 19 '22 07:10

fideloper