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.

Autoloader Namespace

Hello everyone,

I am trying to use the Phalcon Autoloader and include my custom Namespace I registered my namespace wiht "$loader->registerNamespaces()" and when dumping the information afterwards it seems like it worked: array(1) { ["woh"]=> array(1) { [0]=> string(8) "app/src/" } }

But when I try to use new woh\AccessRouteControl(); I just get Fatal error: Uncaught Error: Class 'woh\AccessRouteControl' not found

the Class is located in /app/src/AccessRouteControl.php

What am I doing wrong? Best Regards



1.9k

can you post some part files like $loader->registerNamespaces() and your controller ?



1.9k
edited May '17

in your bootstrap :

$namespace =  [
    'woh'  => APP_PATH . '/.../Controller/',
];

$loader = new Loader();

$loader->registerNamespaces($namespace);

$loader->register();
use woh\AccessRouteControl;

class MyController
{
    public function indexAction()
    {
        $obj = new AccessRouteControl();
    }

but if you use this AccessRouteControl in the bootstrap, before

$this->setApplication(new Application());
$this->setDi($di));

I think it's not registered yet



1.9k

to registered namespace before bootstrap init, I use something like this

spl_autoload_register(function ($class) {

    // project-specific namespace prefix
    $prefix = 'woh\\AccessRouteControl\\';

    // base directory for the namespace prefix
    $base_dir = __DIR__ . '/';

    // does the class use the namespace prefix?
    $len = strlen($prefix);
    if (strncmp($prefix, $class, $len) !== 0) {
        // no, move to the next registered autoloader
        return;
    }

    // get the relative class name
    $relative_class = substr($class, $len);

    // replace the namespace prefix with the base directory, replace namespace
    // separators with directory separators in the relative class name, append
    // with .php
    $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';

    // if the file exists, require it
    if (file_exists($file)) {
        require $file;
    }
});
edited May '17
$di->setShared('loader', function () {
    $loader = new Loader();
    $config = $this->getConfig();

    $loader->registerDirs(
        [
            $config->application->controllersDir,
            $config->application->modelsDir,
            $config->application->formsDir,
        ]
    );
    $loader->registerNamespaces(
        [
            'woh' => 'app/src/'
        ]
    );
    $loader->register();

    return $loader;
});

Thats my autoloader part in the bootstrap. I just want to use the autoloader in my Controller so there is no need for me to register it before bootstrap init.

and in my Controller I try to use it like this:

$acr = new woh\AccessRouteControl();


1.9k
Accepted
answer
edited May '17

Solved.

As taken from the Documentation I thought a relative path would work: https://docs.phalconphp.com/en/3.0.2/reference/loader.html

To solve my problem I just had to use the full path:

    $loader->registerNamespaces(
        [
            'woh' => APP_PATH.'/app/src/'
        ]
    );

edit: I just took an probably outdated phalcon project skeleton generated by the developer tools, but you are right, probably its better to use the Autoloader without the DI because there is no reason to share it.