0

Hello all im trying to load multiple libraries which is in different folders in library folder using namespaces but i keep getting not found

My directory structure is like this

app/
   controllers/
   models/       
   library/
   views/

My loader.php is like this

$loader = new \Phalcon\Loader();

/**
 * We're a registering a set of directories taken from the configuration file
 */

$loader->registerNamespaces(array(
    'Test\Name'        => __DIR__ . "/../library/",
));

$loader->registerDirs(
    array(
        $config->application->controllersDir,       
        $config->application->modelsDir
    )
)->register();

And my basecontroller is trying to call like this

$var = new Test\Name\functions();

and btw the file functions in library is like this

class functions extends Phalcon\Mvc\User\Component
{


    public function __construct()
    {
    }

    public function initialize()
    {
    }

    public function checking(){

        echo 'checks';

    }

}

i Keep getting

Fatal error: Class 'Test\Name\functions' not found in C:\wamp\www\app\controllers\ControllerBase.php on line 38

Any help is appreciated guys thnx

mega-crazy
  • 838
  • 2
  • 17
  • 36

3 Answers3

1

I think that your class should have:

namespace Test\Name;

class functions extends Phalcon\Mvc\User\Component
{
// ...  rest of it

on top.

I would also make this configuration:

$loader->registerDirs(
    array(
        $config->application->controllersDir,       
        $config->application->modelsDir,
        __DIR__ . "/../library/",
    )
)->register();

So your class would be in (also I would rename your class to Functions:

app/library/Test/Name/Functions.php

So it would be obvious that your Functions class is in Test\Name namespace.

jodator
  • 2,445
  • 16
  • 29
0

Having trouble finding the discussion that clued me in but, my understanding is that when using namespaces, you use namespaces. When using directories and other rules, you don't use namespaces.

Namespaces are faster so probably best to just stick with them, dropping the registerDirs as they are superfluous and mean the same thing as the namespaces:

library\Test\Name.php

becomes:

$loader->registerNamespaces(array(
    'Apps\Module\Controllers'   => $config->application->controllersDir,
    'Apps\Module\Models'        => $config->application->modelsDir,
    'Test'        => __DIR__ . "/../library/Test",
));

Then available as Test\Name.

MrYellow
  • 426
  • 6
  • 23
0
$loader->registerNamespaces(array(
    'App'        => __DIR__ . "/../library/",
), true);

it will merge all sub dir, if you librarry dir architecture like this

library - Test - Name.php

You can call new \App\Test\Name();