Problems with Router and Multi Module

Hi!

i'm programming a web with backend access and API, and I want to use multi-module app with shared model.

I want acces api via:

  • xxxx.com/api/controller/action/params

And access backend via:

  • myweb.com/controller/action/params

My router file is:

<?php

use Phalcon\Mvc\Router;

$di->set('router', function () {

    $router = new Router(false);

    $router->removeExtraSlashes(true);

    /*$router->setDefaultModule('backend');
    $router->setDefaultController('index');
    $router->setDefaultAction('index');*/

    $router->add('/api/',[
        'namespace' => 'MyModule\Api\Controllers',
        'module' => 'api',
        'controller' => 'index',
        'action' => 'index'
    ]);

    $router->add('/api/:controller',[
        'namespace' => 'MyModule\Api\Controllers',
        'module' => 'api',
        'controller' => 1,
        'action' => 'index'
    ]);

    $router->add('/api/:controller/:action',[
        'namespace' => 'MyModule\Api\Controllers',
        'module' => 'api',
        'controller' => 1,
        'action' => 2
    ]);

    $router->add('/api/:controller/:action/:params',[
        'namespace' => 'MyModule\Api\Controllers',
        'module' => 'api',
        'controller' => 1,
        'action' => 2,
        'params' => 3
    ]);

    $router->add('/',[
        'namespace' => 'MyModule\Backend\Controllers',
        'module' => 'backend',
        'controller' => 'index',
        'action' => 'index'
    ]);

    $router->add('/:controller',[
        'namespace' => 'MyModule\Backend\Controllers',
        'module' => 'backend',
        'controller' => 1,
        'action' => 'index'
    ]);

    $router->add('/:controller/:action',[
        'namespace' => 'MyModule\Backend\Controllers',
        'module' => 'backend',
        'controller' => 1,
        'action' => 2
    ]);

    $router->add('/:controller/:action/:params',[
        'namespace' => 'MyModule\Backend\Controllers',
        'module' => 'backend',
        'controller' => 1,
        'action' => 2,
        'params' => 3
    ]);

    return $router;
});

I only have problems with API access, in the log appears:

PHP Fatal error:  Uncaught Phalcon\Mvc\Dispatcher\Exception: MyModule\Backend\Controllers\ApiController handler class cannot be loaded in C:\wamp\www\mynewapp\public\index.php:96
Stack trace:
#0 [internal function]: Phalcon\Mvc\Dispatcher->_throwDispatchException('MyModule\\Backend...', 2)
#1 [internal function]: Phalcon\Dispatcher->dispatch()
#2 C:\wamp\www\mynewapp\public\index.php(96): Phalcon\Mvc\Application->handle()
#3 C:\wamp\www\mynewapp\public\index.php(101): Application->main()
#4 {main}
  thrown in C:\wamp\www\mynewapp\public\index.php on line 96

I'm using PHP7 and Nginx

What am I doing wrong?

Thank you very much in advance



1.8k
edited May '18

Hi!

I think the problem was in the order of the declaration of the routes.

With the following configuration, it works

// Registering a router
        $di->set('router', function () {

            $router = new Router();

            $router->setDefaultController("index");
            $router->setDefaultAction("index");

            //frontend
            $router->add('/:controller/:action/:params', [
                'module'     => 'frontend',
                'controller' => 1,
                'action'     => 2,
                'params'    => 3
            ]);

            $router->add('/:controller/:action', [
                'module'     => 'frontend',
                'controller' => 1,
                'action'     => 2,
            ]);

            $router->add('/:controller[/]{0,1}', [
                'module'     => 'frontend',
                'controller' => 1,
                'action'     => 'index',
            ]);

            $router->add('/', [
                'module'     => 'frontend',
                'controller' => 'index',
                'action'     => 'index',
            ])->setName('frontend');

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

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

            $router->add('/api/:controller[/]{0,1}', [
                'module'     => 'api',
                'controller' => 1,
                'action'     => 'index',
            ]);

            $router->add('/api[/]{0,1}', [
                'module'     => 'api',
                'controller' => 'index',
                'action'     => 'index',
            ])->setName('api');

            return $router;
        });