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

[Codeception] MultiModule Phalcon 3 Dispather Don't recognize routes

Hi, I using Codeception and MultiModule Phalcon 3.0.3

i have declared route "login" fowardind to module session, controller index and action login. When i type in browser "/login" all work perfectly.

But when in FunctionalTest i use

<?php
$I->click('Login');

then Dispatcher send me Error about LoginController cannot be loaded

when i change href for "session/index/login" it work corectly. So: what i can do wrong? PS. I dont have bootstrap for cli. in codeception i define to load bootstrap_web.php

Im guessing that Codeception have his own dispatcher which doesn't have default configuration like your dispatcher or something like this. Didn't use really codeception much.



7.6k
edited Jan '17

Wojtek, this is phalcon exception. Below what codeception show me

<?php
1) IndexCept: Perform actions and see result
 Test  tests/functional/IndexCept.php

  [Phalcon\Mvc\Dispatcher\Exception] Portal\Modules\Frontend\Controllers\LoginController handler class cannot be loaded  

Scenario Steps:

 3. $I->click({"link":"Login"}) at tests/functional/IndexCept.php:6
 2. $I->see("Login","a") at tests/functional/IndexCept.php:5
 1. $I->amOnPage("/session/index/login") at tests/functional/IndexCept.php:4


7.6k

more: i make acceptance test when i use "/login" path and it works because acceptance tests based in PhpBrowser module, not on Phalcon module.



7.6k
Accepted
answer
edited Jan '17

Solution:

<?php 
// app/config/routes.php

$router = $di->getRouter();

foreach ($application->getModules() as $key => $module) {
    $namespace = preg_replace('/Module$/', 'Controllers', $module["className"]);
    $router->add('/' . $key . '/:params', [
        'namespace' => $namespace,
        'module' => $key,
        'controller' => 'index',
        'action' => 'index',
        'params' => 1
    ])->setName($key);
    $router->add('/' . $key . '/:controller/:params', [
        'namespace' => $namespace,
        'module' => $key,
        'controller' => 1,
        'action' => 'index',
        'params' => 2
    ]);
    $router->add('/' . $key . '/:controller/:action/:params', [
        'namespace' => $namespace,
        'module' => $key,
        'controller' => 1,
        'action' => 2,
        'params' => 3
    ]);

    $moduleRoutes = APP_PATH . '/modules/' . $key . '/routes.php';
    if (file_exists($moduleRoutes)) {
        require $moduleRoutes;
    }
}

The problem was require. When I use require instead require_once everything goes well.