Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

default namespace not working in multi module until set a default module

Hi

I registered multi modules but controllers of modules not working until I set a default module in router.

and for example, I set a default module in router:

$router->setDefaultModule("frontend");

now other modules not working and throw an error:

Fatal error: Uncaught exception 'Phalcon\Mvc\Dispatcher\Exception' with message 'Modules\Frontend\Controllers\AdminController handler class cannot be loaded

although I set a router for 'admin':

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


82.2k

You can put your routes in a test script like described here to see what parameters are processed according to the URI passed to your application. http://docs.phalconphp.com/en/latest/reference/routing.html#testing-your-routes

The result is same as what described in error. it tries to run admin controller in Frontend module!



82.2k
edited Jul '14

I'm running the following script, here: http://test.phalconphp.com/r.php and it works properly, could you please update it to reproduce the problem?

<?php

$router = new Phalcon\Mvc\Router();

$router->setDefaultModule("frontend");

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

$router->handle('/admin');

//Check if some route was matched
if ($router->wasMatched()) {
    echo 'Module: ', $router->getModuleName(), '<br>';
    echo 'Controller: ', $router->getControllerName(), '<br>';
    echo 'Action: ', $router->getActionName(), '<br>';
} else {
    echo 'The route wasn\'t matched by any route<br>';
}

result:

Module: backend
Controller: index
Action: index

Fatal error: Uncaught exception 'Phalcon\Mvc\Dispatcher\Exception' with message 'Apps\Frontend\Controllers\AdminController handler class cannot be loaded

and I'm so confused. router handle returns true module but error says about other module



82.2k
edited Jul '14

Is $_GET["_uri"] equals to /admin? Maybe is it caused by a forward?

edited Jul '14

'_uri' is undefined in GET.

Edit:

I put print_r($_GET) in ControllerBase of frontend module and _url (not _uri) is equal to '/admin'. it seems router wont work when application handled.

Is there any simple multi modules project with a controllerBase? This problem wasted all of my time!

edited Jul '14

After hours and hours I found this:

I added this route:

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

now if I go to site.com/admin backend module called but for every sub addresses (admin/*) frontend (default) module called!

then I add this routes too:

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

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

now every address in admin works perfectly and use backend module. but now the frontend site won't work correctly! (All addresses except /admin/* goes to index contrller of Frontend) and if I add this routes too:

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

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

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

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

now backend module (admin and admin/*) do not work!! :)) (frontend module called)

I'm so confused, please help me!



9.6k
Accepted
answer
edited Jul '14

ooops!

I just disabled default route before this. now I enabled it $router = new \Phalcon\Mvc\Router(true); and everyhting works perfectly!



501

I tried evething, but not working.

When I call /admin/everythingelse I got:

Phalcon\Mvc\Dispatcher\Exception: App\Frontend\Controllers\AdminController handler class cannot be loaded

Oh god, found this issue.

When application handling a request, it passes namespace to the dispatcher (https://github.com/phalcon/cphalcon/blob/master/phalcon/mvc/application.zep#L267), but namespace will be empty if not defined in route and default will be used ($dispatcher->setDefaultNamespace('App\Modules\Frontend\Controllers');)

Is this a bug?