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

Problem with loading namespaces

I have a multi-module application. It works perfect on my Windows 8 dev-machine (nginx, php-fastcgi), but when I uploaded it to the production server (ubuntu, php-fpm) Phalcon seems to not load namespaces correctly. While I am on the production server, I can add the namespaces in the "registerNamespaces" method, then it seems to work OK. Why does it not load the namespaces from the modules? Any hints of where to start troubleshooting?

First error message:

    Array ( [type] => 1 [message] => Class 'KitCloud\Module\Core\Controller\BaseController' not found [file] => /var/www/kit/kitcloud/dev/module/dashboard/controller/IndexController.php [line] => 5 ) 

When I add the following in my loader.php registerNamespaces method that error is replaced by another one.

    'KitCloud\Module\Core\Controller' => $config->application->moduleDir.'/core/controller/',

Phalcon version: 1.3.2

Bootstrap:

    $application = new \Phalcon\Mvc\Application($di);
    $application->registerModules(\KitCloud\Module\Core\Helper\Misc::object_to_array($config->application->modules));
    echo $application->handle()->getContent();

Modules array from config:

        'modules' => array(
            'core' => array(
                'className' => 'KitCloud\Module\Core\Module',
                'path'      => '../module/core/Module.php',
                'title'     => _('Core')
            ),
            'dashboard' => array(
                'className' => 'KitCloud\Module\Dashboard\Module',
                'path'      => '../module/dashboard/Module.php',
                'title'     => _('Dashboard')
            ),
        )

Module.php:

    public function registerAutoloaders()
    {

        $loader = new Loader();

        $loader->registerNamespaces(
            array(
                'KitCloud\Module\Core\Controller' => '../module/core/controller/',
                'KitCloud\Module\Core\Model'      => '../module/core/model/',
                'KitCloud\Module\Core\Helper'      => '../module/core/helper/',
            )
        );

        $loader->register();
    }


7.9k
Accepted
answer

try to match your namespace with your folder (case sensitive)

for example if your namespace KitCloud\Module\Core\Controller' your folder will be '../Module/Core/Controller/'

I was encountered this issue before but i managed to solved it use that solution above



5.1k
edited Sep '14

@Atriedes - I suppose this should not be the issue (I'm using lowercase folders).

@fl0pp - Did you try with absolute path or double backslash like Foo\\Bar\\Namespace?



7.9k

@arius86 - it was my issue because I use merged namespace loader (using PSR-4 standard)

$loader->registerNamespaces(
            array(
                'Vendor => '../app'
            ), true
        );

It work on ubuntu vagrant box in pc not it refused to work on ubuntu vagrant box in my laptop

@arius86 I tried absolute path, same problem. I also tried double backslash with no effect. @Prasetyo I tried uppercase the folder names just in the namespace registration, with no luck. But when I renamed the folder to uppercase and uppercased the namespaces it worked! Thanks for noting! I still think it is strange that i works on windows and not linux. Is it because of PHP or Phalcon?



7.9k

Yes namespace and dirname must be exactly same (case sensitive) i guesst its OS problem linux is case sensitve and i think windows is not case sensitive