We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

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.

Module exception if does not exist

Hello,

I have set the Route:

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

When I enter URL to the browser, for example: auth/login/index and module under this URL does not exist, so it throws an exception:

Phalcon\Mvc\Application\Exception: Module 'auth' isn't registered in the application container

How can I catch this exception?



7.0k
edited Jul '15

This exception becomes when your module isn't registered.

https://docs.phalcon.io/en/latest/reference/applications.html#multi-module

This should help you, works fine for me.



1.1k
edited Jul '15

Ok, I try it code below:

try {

    echo $application->handle()->getContent();

} catch (\Exception $e) {

   if (preg_match('/Module (.*) isn\'t registered in the application container/', $e->getMessage()) == 1) {

        $di->get('dispatcher')->forward(array(
            'namespace' => 'App\Controllers',
            'controller' => 'error' ,
            'action' => 'show404'
        ));

        return false;
    }
}

But i'm still cannot send it to use Error controller. Does anyone help?



7.0k

Did you registered any module?



1.1k
Accepted
answer
edited Jul '15

Ok, i have solution:

$router->add('/:module/:controller/:action/:params', [
      'module' => 1,
      'controller' => 2,
      'action' => 3,
      'params' => 4
])->beforeMatch(function($uri) use ($application) {

      $modules = $application->getModules();

      $moduleName = array_filter(explode('/', $uri))[1];

      if(!isset($modules[$moduleName]))
          return false;

      return true;
});

In beforeMatch method I check If module exist.