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

Multi Modules + Annotations Routing

Hello,

I am developing a multi modules application and following the tuts on https://docs.phalcon.io/en/latest/reference/applications.html#multi-module

So far the app works just fine, but when I integrated it with Annotations Routing, it keeps showing

Class Multiple\Backend\Controllers\IndexController does not exist

Here is my change to the tutorial files:

In the bootstrap file:

//Specify routes for modules
$di->set('router', function () {

    $router = new Router\Annotations();

    $router->addModuleResource('backend', 'Multiple\Backend\Controllers\Index', '/api');

    return $router;
});

In the apps/backend/controllers/IndexController.php

<?php
namespace Multiple\Backend\Controllers;

/**
* @RoutePrefix("/api")
*/
class IndexController extends \Phalcon\Mvc\Controller
{
    /**
    * @Get("/test")
    */
    public function indexAction()
    {
        echo "stream";
    }
}

Yet when I access via the URL:

https://myapp/index.php?_url=/api/test

It always shows:

Class Multiple\Backend\Controllers\IndexController does not exist

Is there anything I've done wrong with the Annotations?

You have to register the namespaces of the controllers in a global autloader. This is what I had did so far, but unfortuantely, modules are loaded after routing execution.

In you "loader.php" file, under "registerNamespaces", you have to register the classes namepsaces, like:

$loader->registerNamespaces([ 'Multiple\Backend\Controllers' => APP_PATH . '/modules/backend/controllers' ]);

It should work.