Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

I can't load library simple_html_dom

I want to study simple_html_dom using Phalconphp. But I can't load library Please, Help me.

simple_html_dom.php located at \app\library\simplehtmldom

config.php

<?php use Phalcon\Config;

return new Config(

[

    'database' => [

        'adapter'     => 'XXXXXX',

        'host'        => 'XXXXXX',

        'username'    => 'XXXXXX',

        'password'    => 'XXXXXX',

        'dbname'      => 'XXXXXX',

        'charset'     => 'XXXXXX'

    ],

    'application' => [

        'baseUri'        => '/',

        'cacheDir'       => __DIR__ . '/../../app/cache/',

        'controllersDir' => __DIR__ . '/../../app/controllers/',

        'layoutDir'      => __DIR__ . '/../../app/views/_templates/',

        'libraryDir'     => __DIR__ . '/../../library/',

        'modelsDir'      => __DIR__ . '/../../app/models/',

        'partialDir'     => __DIR__ . '/../../app/views/_templates/current/part/',

        'pluginsDir'     => __DIR__ . '/../../app/plugins/',

        'serviceDir'     => __DIR__ . '/../../app/service/',

        'viewsDir'       => __DIR__ . '/../../app/views/'

    ]

]

);

loader.php

<?php

$loader = new \Phalcon\Loader();

/**

  • We"re a registering a set of directories taken from the configuration file */ $loader->registerDirs([

    $config->application->controllersDir,

    $config->application->modelsDir,

    $config->application->libraryDir

]);

$loader->registerNamespaces(

[

    // Controllers

    "App\Controllers"                     => $config->application->controllersDir,

. . .

    //     

    "App\Models\User"                     => $config->application->modelsDir . "user/",

    "Phalcon"                             => __DIR__ . "/../../app/library/incubator/Library/Phalcon/",

    "SimpleHtmldom"                       => $config->application->libraryDir . "simplehtmldom/"

]

);

$loader -> register();

IndexController.php

<?php

namespace App\Controllers\Seo;

class IndexController extends ControllerBase{

public function indexAction(){

    $simpleHtmldom = new \app\library\SimpleHtmldom\simple_html_dom();

}

}

The error is belowe

Fatal error: Uncaught Error: Class 'app\library\SimpleHtmldom\simple_html_dom' not found in /var/www/html/study/app/app/controllers/seo/IndexController.php:8 Stack trace: #0 [internal function]: App\Controllers\Seo\IndexController->indexAction() #1 [internal function]: Phalcon\Dispatcher->callActionMethod(Object(App\Controllers\Seo\IndexController), 'indexAction', Array) #2 [internal function]: Phalcon\Dispatcher->dispatch() #3 /var/www/html/study/app/public/index.php(52): Phalcon\Mvc\Application->handle() #4 {main} thrown in /var/www/html/study/app/app/controllers/seo/IndexController.php on line 8

Please help me.



68.0k
Accepted
answer

The path of the PHP file has nothing to do with it's namespace in code.

simple_html_dom resides in the root namespace, so register the proper file path, then instantiate with the proper namespace.

// simple_httml_dom has only one source php file, register it as a file
$loader->registerFiles([
    $config->application->libraryDir . "simplehtmldom/simple_html_dom.php",
]);
// Instantiate from root namespace
$dom = new \simple_html_dom();