Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhalconPHP - Cannot load controllers if they are namespaced with Loader

Tags:

php

phalcon

Before I namespaced my application (test app), everything worked fine. But after I started namespacing the controllers, and everything and loading the namespaces like this:

    $loader = new Loader();
    $loader->registerNamespaces(
        array(
            'Application\Controllers' => 'Application/Controllers/'
        )
    )->register();

I get the error Exception: IndexController handler class cannot be loaded

My namespace I entered is correct:

namespace Application\Controllers;

use Phalcon\Mvc\Controller;

class IndexController extends Controller {

    public function indexAction() {
        echo "Hello World";
    }
}

img
(source: gyazo.com)

I managed to fix it by adding a default namespace to the Router:

$router = new Router();
$router->setDefaultNamespace('Application\Controllers');

But that might cause me further problems, because namespace loader doesn't seem to work.

What is wrong?

like image 828
Artemkller545 Avatar asked Mar 16 '23 10:03

Artemkller545


2 Answers

Nothing is wrong, the namespace registration both in the Loader and Router are required steps.

At first instance you configured only your loader. So if you test somewhere:

$controller = new Application\Controllers\IndexController(); It will work.

However you didn't tell yet to your router that all your routes should point to the Application\Controllers namespace. So it attempts to find the \IndexController class which is indeed non existent.

like image 99
kbtz Avatar answered Mar 19 '23 04:03

kbtz


I think you just have to set the root of the namespace and then the Loader will do the scaffolding:

$loader = new Loader();
$loader->registerNamespaces(
    array(
        'Application' => 'Application'
    )
)->register();

And are you sure that you are getting the Application folder correct? try:

$loader = new Loader();
$loader->registerNamespaces(
    array(
        'Application' => '~/Application/'
    )
)->register();
like image 26
taxicala Avatar answered Mar 19 '23 03:03

taxicala