Dispatch Not working

I have a multi-module type setup going. As long as my module>controller>action exists, everything seems to be fine. I've just recently started looking into showing a 404 page when someone goes to /somerandompage and that module, controller or action doesn't work. Can someone explain to me why the dispatcher below doesn't work. I need it there to catch when the module doesn't exist.

Take a look at the following:

index.php

<?php

// Root path must be defined
define('PATH_ROOT', realpath('..') . DIRECTORY_SEPARATOR);

try {

require PATH_ROOT . 'app/config/bootstrap.php';

$application = new \Phalcon\Mvc\Application($di);

$application->registerModules($config->getModulesList());

echo $application->handle()->getContent();
}
catch (\Exception $e) {

$di->get('dispatcher')->forward([
'controller' => 'error',
'action' => 'show404',
]);

}

routes.php

    $di->set('router', function() use ($di) {

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

    $router->removeExtraSlashes(true);
    $router->setDefaultModule('index');
    $router->setDefaultController('index');
    $router->setDefaultAction('index');

    $router->notFound([
        'module' => 'index',
        'controller' => 'error',
        'action' => 'show404',
    ]);

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

    $router->add('/:module', [
        'module' => 1,
    ]);

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

    return $router;
});


10.6k

I got the dispatcher to work by using it and passing it into my function:

    use Phalcon\Http\Response;
    use Phalcon\Mvc\Dispatcher;
    public function beforeExecuteRoute(Dispatcher $dispatcher) {
        $user = $this->session->get('auth');
        $action = $dispatcher->getActionName();
        $response = new Response();
        if (empty($user)) {
            $response->redirect("/login",false);
            $response->send();
        }
        if ($user->role != 'vendor' && in_array($action,$this->vendorActions)) {
            $response->redirect("/",false);
            $response->send();
        }
    }