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

Namespace IndexController handler class cannot be loaded

Hey Guys,

I'm currently rewriting my whole Project from scratch and want to organize my Controllers, Models, Plugins etc. with Namespaces.

So everything has a namespace like Test\Blubb\Controllers or Test\Blubb\Model or Test\Bla\Controllers


    <?php
    namespace Test\Blubb\Controllers;

    class IndexController extends ControllerBase{
        public function indexAction {
        }
    }

Added a Router with right namespaces for my diffrent routes and registered within DI. Also my namespaces are loaded with correct path with the Loader.

    $router = $di->getRouter();

    $router->add('/:controller', [
    'namespace'  => $config->namespaces->controllers,
    'controller' => 1
    ]);

    $router->add('/:controller/:action', [
    'namespace'  => $config->namespaces->controllers,
    'controller' =>  1,
    'action'     =>  2
    ]);

    $router->add('/:controller/:action/:params', [
    'namespace'  => $config->namespaces->controllers,
    'controller' => 1,
    'action'     => 2,
    'params'     => 3
    ]);

    $router->handle();
    $loader->registerNamespaces(
    [
        $config->namespaces->controllers => '../controllers/',
        $config->namespaces->models => '../models/',
        $config->namespaces->plugins => '../library/plugins/',
        $config->namespaces->components => '../library/components/',
    ])->register();

Now when i try to access the page I get this error:

Test\Blubb\Controllers\IndexController handler class cannot be loaded

0 [internal function]: Phalcon\Mvc\Dispatcher->_throwDispatchException('Test\Blubb\Contr...', 2)

1 [internal function]: Phalcon\Dispatcher->_dispatch()

2 [internal function]: Phalcon\Dispatcher->dispatch()

3 ....../public/index.php(42): Phalcon\Mvc\Application->handle()

4 {main}

It uses the correct namespace but cant find my Controller. Double checked my spelling for mistakes and searched through the net, no solutions that i found worked :/

Someone got an idea what might be the problem?



43.9k

Hi,

are you sure that $config->namespaces->controllers look something like "Test\Blubb\Controllers" ?

I am. They're looking like this:

    ....
    'namespaces' => [
        'controllers' => 'Test\\Blubb\\Controllers',
        'models' => 'Test\\Blubb\\Models',
        ....
    ]
    ....


43.9k

well, maybe a file path problem here:

$config->namespaces->controllers => '../controllers/',

or a wrong namespace declaration in IndexController itself ...

I changed the file path to use APP_PATH insted of those dots now it seems to work...but really strange And the namespace declaration is correct



43.9k

It's always a good idea to use APP_PATH !!