We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

not loading directory classes in loader

I am trying to load directory classes in universal loader class but its not loading.

returning fatal error class not found.

$loader = new \Phalcon\Loader();

// Register some directories

$loader->registerDirs(['/app/library'])->register();

$class = new class();



43.9k

Hi,

namespace problem, wrong library directory location (the loader can't find app/library) ... ?

I have tried with the location but not sure about namespace shall I need to register it??

Thanks Shivanshu On 16 Dec 2014 15:36, "le51" [email protected] wrote:



43.9k
Accepted
answer

if your classes in app/library are namespaced, you should register them: https://docs.phalcon.io/en/latest/reference/loader.html#registering-namespaces

but maybe the loader just can't find the ap/library directory. Bellow a classic MVC app structure:

app/

config/

config.php

service.php

loader.php

controllers/

models/

.....

then:


// in config.php:
return new \Phalcon\Config(array(
....

    'application' => array(
        'controllersDir' => __DIR__ . '/../../app/controllers/',
        'modelsDir'      => __DIR__ . '/../../app/models/',
        'viewsDir'       => __DIR__ . '/../../app/views/',
        'pluginsDir'     => __DIR__ . '/../../app/plugins/',
        'libraryDir'     => __DIR__ . '/../../app/library/',
        'cacheDir'       => __DIR__ . '/../../app/cache/',
        'baseUri'        => '/',
    ),

.....

// in loader.php:

$loader = new \Phalcon\Loader();

/**
 * We're a registering a set of directories taken from the configuration file
 */
$loader->registerDirs(
    array(
        $config->application->controllersDir,
        $config->application->modelsDir,
         $config->application->pluginsDir,
          $config->application->libraryDir
    )
)->register();
edited Dec '14

Thanks its works fine was problem with namespace. Do you have any idea how can I create interface instance ?

I am trying to do something like

$di->get('UsersController', [ 'Service\IUsersRepository' ]);

class UsersController extends AbstractController
{
    private  $users;
    public function __construct(IUsersRepository $usersRepository)
    {
        $this->users = $usersRepository;
    }
}

Is there any way to bind interface with class ?

thanks