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?



3.0k
edited Jul '15

This exception becomes when your module isn't registered.

https://docs.phalconphp.com/en/latest/reference/applications.html#multi-module

This should help you, works fine for me.



608
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?



3.0k

Did you registered any module?



608
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.