0

I need some help with a multi module Phalcon application. I followed the instructions as per https://github.com/phalcon/mvc/tree/master/multiple but cannot get the variable routing working for the non default module.

$router = new Router();
$router->setDefaultModule("admin");
$router->setDefaultAction('index');

This works for the admin module:

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

This works only works for the api module (the not default module) when set manually:

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

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

But this won't work for the api module:

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

I then get an error like below when I use /api or /api/user:

\www\site\public\index.php:104:string 'admin\controllers\ApiController handler class cannot be loaded'

But when I access /api/user/index it works. It looks like for the not default module it forgets the setDefaultAction

Alvin Bakker
  • 1,456
  • 21
  • 40

3 Answers3

0

You're missing routes with default controller, actions:

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

Phalcon needs routes to be strictly specified, otherwise it's not going to resolve them. We're paying this price for routing's high performance.

Luke
  • 2,350
  • 6
  • 26
  • 41
0

Try to set namespace

'namespace' => 'App\Modules\Api\Controllers']
rzx10r
  • 134
  • 1
  • 1
  • 9
0

Your first pattern is a catch-all which sends all routes to the admin controller. Have you tried putting the admin module route at the end?

Alternatively, you could use a pattern that includes the module:

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