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 phalcon and annotations in multi module app

Hello everyone

I'm trying to make an app with multiple modules, but I have problems with the API module, I'm trying to log Use this module to work routes in a simple way, but I manage to make it work.

The map directory is:

Directorios

In public\index.php

use Phalcon\Mvc\Application; error_reporting(E_ALL); try {

/**
 * The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework
 */
$di = new \Phalcon\DI\FactoryDefault();

/**
 * Registering a router
 */
$di->set('router', function() {
    $router = new \Phalcon\Mvc\Router\Annotations(true);
    $router->setDefaultModule("frontend");

    $router->add("inicio", array(
        'module'     => 'frontend',
        'controller' => 'index',
        'action'     => '',
    ));

    $router->add("/login/", array(
        'module'     => 'backend',
        'controller' => 'login',
        'action'     => 'index',
    ));

    $router->add("/login", array(
        'module'     => 'backend',
        'controller' => 'login',
        'action'     => 'index',
    ));

    $router->add("/login/:action", array(
        'module'     => 'backend',
        'controller' => 'login',
        'action'     => 1,
    ));     

    $router->add("/admin/:controller/:action", array(
        'module'     => 'backend',
        'controller' => 1,
        'action'     => 2,
    ));

    $router->add("/api/", array(
        'module'     => 'api',
        'controller' => 'index',
        'action'     => '',
    ));

    $router->add("/api/:controller/", array(
        'module'     => 'api',
        'controller' => 1,
    )); 

    $router->add("/api/:controller/:action", array(
        'module'     => 'api',
        'controller' => 1,
        'action'     => 2,
    ));

    $router->add("/api/:controller/:action/:params", array(
        'module'     => 'api',
        'controller' => 1,
        'action'     => 2,
        'params'     => 3,
    )); 

    return $router;
});

/**
 * The URL component is used to generate all kind of urls in the application
 */
$di->set('url', function() {
    $url = new \Phalcon\Mvc\Url();
    $url->setBaseUri('/hmvc/');
    return $url;
});

/**
 * Start the session the first time some component request the session service
 */
$di->set('session', function() {
    $session = new \Phalcon\Session\Adapter\Files();
    $session->start();
    return $session;
});

/**
 * Handle the request
 */
$application = new \Phalcon\Mvc\Application();

$application->setDI($di);
/**
 * Register application modules
 */
$application->registerModules(array(
    'frontend' => array(
        'className' => 'Modules\Frontend\Module',
        'path' => '../apps/frontend/Module.php'
    ),
    'backend' => array(
        'className' => 'Modules\Backend\Module',
        'path' => '../apps/backend/Module.php'
    ),
    'api' => array(
        'className' => 'Modules\API\Module',
        'path' => '../apps/api/Module.php'
    )   
));

echo $application->handle()->getContent();} catch (Phalcon\Exception $e) {
echo $e->getMessage();} catch (PDOException $e){
echo $e->getMessage();}

apps/api/Module.php

public function registerAutoloaders() {

    $loader = new \Phalcon\Loader();

    $loader->registerNamespaces(array(
        'Modules\API\Models' => __DIR__ . '/models/',
        'Modules\API\Controllers' => __DIR__ . '/controllers/',         
        'Modules\API' => __DIR__ . '/',
    )); 

    $loader->register();

    $loader->registerDirs(
        array(
            __DIR__ . '/controllers/',
            __DIR__ . '/models/'
        ));
    $loader->register();
}

public function registerServices($di)
{
    //Registering a dispatcher
    $di->set('dispatcher', function() {
        $dispatcher = new Dispatcher();
        $dispatcher->setDefaultNamespace("Modules\API\Controllers");
        return $dispatcher;
    });

    //Registro las rutas
    $router = $di->get('router');

    $router->addGet(
        "/api/{club:[a-z\-]+}/:controller/:params",
        array(
            "controller" => 1,
            "params"     => 2, // :params
        )
    );

    //Registering the view component
    $di->set('view', function() {
        $view = new View();
        $view->setViewsDir(__DIR__ . '/views/');
        return $view;
    });
    /**
     * Database connection is created based in the parameters defined in the configuration file
     */
    $config = include __DIR__ . "/config/config.php";
    $di->set('db', function() use ($config) {
        return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
            "host" => $config->database->host,
            "username" => $config->database->username,
            "password" => $config->database->password,
            "dbname" => $config->database->name
        ));
    }); 
}

apps/Api/Controllers/SociosController.php

/**

  • @RoutePrefix("api/socios") */ namespace Modules\API\Controllers; class SociosController extends ControllerBase {
    /**
  • @Get("/") */ public function indexAction() { echo "test"; }
public function sociosAction()
{
}

When I try the enter the URL https://localhost/hmvc/api/socios

"Modules\Frontend\Controllers\ApiController handler class cannot be loaded"

Thanks for your help

I can't see it from your code so I'll ask just in case: is your Module.php in your Modules\API namespace?

I can't see it from your code so I'll ask just in case: is your Module.php in your Modules\API namespace?

Yes is in the namespace "Modules\API"