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

Multimodule annotation routing

Hi everyone, I've seen there are lots of discussions about annotations routing with a multi module setup, however reading them didn't fix my problem.

I am not able to make the application work with the above mentioned setup, and the exception stack trace and message that I get on screen are the follow

#0 [internal function]: Phalcon\Annotations\Reader->parse('PostsController') 
#1 [internal function]: Phalcon\Annotations\Adapter->get('PostsController') 
#2 [internal function]: Phalcon\Mvc\Router\Annotations->handle(NULL) 
#3 /usr/share/nginx/www/phalcon.multi/public/index.php(49): Phalcon\Mvc\Application->handle() 
#4 {main}

Class PostsController does not exist

This is my current setup on the index.php bootstrap file (located at /public/index.php)

<?php
use Phalcon\Mvc\Router,
    Phalcon\Mvc\Application,
    Phalcon\DI\FactoryDefault, 
    Phalcon\Mvc\Router\Annotations as AnnotationsRouter,
  Phalcon\Loader;

$di = new FactoryDefault();

$loader = new Loader();
$loader->registerNamespaces(
    array(
        'Multiple\Frontend' => '../apps/frontend',
        'Multiple\Frontend\Controller' => '../apps/frontend/controllers', 
    )
);
$loader->register();

// Annotation router test
$di->set('router', function () {
    $router = new AnnotationsRouter(false);
  //$router->addResource('Posts');
    $router->addModuleResource('frontend', 'Posts', '/api/posts');

    //Read the annotations from ProductsController if the uri starts with /api/products
    //$router->addResource('Posts', '/api/posts');

    return $router;
});

try {
    //Create an application
    $application = new Application($di);
    // Register the installed modules
    $application->registerModules(
        array(
            'frontend' => array(
                'className' => 'Multiple\Frontend\Module',
                'path' => '../apps/frontend/Module.php',
            ),
            'backend' => array(
                'className' => 'Multiple\Backend\Module',
                'path' => '../apps/backend/Module.php',
            )
        )
    );

    //Handle the request
    echo $application->handle()->getContent();
} catch(\Exception $e){
    echo $e->__toString() . "<br /><br />";
    echo $e->getPrevious() . "<br /><br />";
    echo $e->getTraceAsString() . "<br /><br />";
    echo $e->getMessage();
}

and this is the PostsController (/apps/frontend/controllers/PostsController.php)

namespace Multiple\Frontend\Controllers;

use Phalcon\Mvc\Controller;

/**
 * @RoutePrefix("/api/posts")
 */
class PostsController extends Controller
{
    /**
     * @Get("/")
     */
    public function indexAction() 
    {
        die('Just GET /');
    }

    /**
     * @Get("/edit/{id:[0-9]+}", name="edit-post")
     */
    public function editAction() 
    {
        echo "Edit action with params";

        var_dump($this->dispatcher->getParams());

        die();
    }
}

What's wrong in my configuration? Any clue?

Thanks



7.9k

have tried include namespace?

 $router->addModuleResource('frontend', 'Multiple\\Frontend\\Controllers\\Posts', '/api/posts');

Yeah, same error :(



12.2k

Hi, please see this repo https://github.com/valVk/ph-test

Do git checkout twomodules

And try to compare with your code.

Tahnks.