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";
}
}
(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?
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With