--' isn't registered in the application container' - Discussion ---
We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

'Module '------' isn't registered in the application container'

This is an exception when the module is not defined. Is there any event to catch that exception? Triying to use the beforeException over dispatcher does not works.



77.7k
Accepted
answer
edited Sep '15

https://github.com/phalcon/cphalcon/blob/phalcon-v2.0.7/phalcon/mvc/application.zep#L159

https://github.com/phalcon/cphalcon/blob/phalcon-v2.0.7/phalcon/mvc/application.zep#L241

Modules are handled before the dispatch loop, so there is no way to catch it there. But since you're the one setting them up in the first place, I think it's one's job to handle unregistered module names... It makes sense; the whole dependency tree is set up depending on a module.

edited Sep '15

You could handle it like this:

/// index.php

try {
    // ...
    echo $application->handle()->getContent();
}
catch(Phalcon\Application\Exception $e) {
    if(preg_match('/^Module \'(.*?)\' isn\'t registered in the application container$/', $e->getMessage(), $match)) {
        // You can get the attempted module name from the router:
        echo '<pre>', var_dump($application->router->getModuleName()), '</pre>';
        // Or the regexp match
        echo '<pre>', var_dump($match[1]), '</pre>';
        // Then handle it as you wish...
        echo $application->handle('/error/e404')->getContent();
    }
    // ...
}

It's not very nice... but works :P

edited Sep '15

Thanks @Lajos i didn't take the time to look at the application-front controller. Yep, I will have to catch it on the application.

Anyway, would be great to fire an event like the beforeException in dispatcher but from the application class. I will post it on gihub and if I fond time I will try to make a PR.

I wrote it quickly on https://github.com/phalcon/cphalcon/pull/10964 2.1.x branch. I didn't test it so if anyone is able to check it would be great. Thanks