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

Intercept Module not found

Hi all, how can I intercept the "exception" Module 'XXXXX' isn't registered in the application container ? Actually I'm using the event Manager, and the router notFound method, but none fires when a module is not found.

        $di->getShared('eventsManager')->attach("dispatch:beforeException", function ($event, \Phalcon\Mvc\Dispatcher $dispatcher, $exception) {

            switch ($exception->getCode()) {
                case \Phalcon\Mvc\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                case \Phalcon\Mvc\Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                    $dispatcher->forward(array(
                        'controller' => 'errors',
                        'action' => 'show404',
                        'namespace' => 'App\Base\Controllers'
                    ));
                    return false;
            }
        });

mmm in your boostrap (index.php)

do you defined your modules fine: (example)

// Register the installed modules
$application->registerModules(
        array(
            'public' => array(
                'className' => 'Frontend\Module',
                'path' => '../app/frontend/Module.php'
            ),
            'private' => array(
                'className' => 'Backend\Module',
                'path' => '../app/backend/Module.php'
            ),
            'common' => array(
                'className' => 'Common\Module',
                'path' => '../app/common/Module.php'
            )
        )
);
echo $application->handle()->getContent();

Yes, they are defined. But when I try to reach a non-existan/non-registered module I want the dispatcher to raise an Exception, the same way it does with action not found. None, the router notfound and the on beforeexception on dispatcher works in this case.

mmm in your boostrap (index.php)

do you defined your modules fine: (example)

// Register the installed modules $application->registerModules( array( 'public' => array( 'className' => 'Frontend\Module', 'path' => '../app/frontend/Module.php' ), 'private' => array( 'className' => 'Backend\Module', 'path' => '../app/backend/Module.php' ), 'common' => array( 'className' => 'Common\Module', 'path' => '../app/common/Module.php' ) ) ); echo $application->handle()->getContent();

Ok, understand!... you must do :

in your index.php

set_exception_handler(function($e) {
    $error = new \Common\Helpers\CoreHelper();
    return $error->handleException($e);
});
set_error_handler(function($errorCode, $errorMessage, $errorFile, $errorLine, $errcontext) {
    $error = new \Common\Helpers\CoreHelper();
    return $error->handleError($errorCode, $errorMessage, $errorFile, $errorLine, $errcontext);
});

on your controller:

public function handleException($e) {
        if (ob_get_level() > 0) {
            ob_end_clean();
        }
        $this->_fnformatHandlerException($e);
        header("Location: /common/error/index.html/600");
    }

    public function handleError($errorCode, $errorMessage, $errorFile, $errorLine, $application = null) {
        if (ob_get_level() > 0) {
            ob_end_clean();
        }
        $this->_fnformatHandlerError($errorCode, $errorMessage, $errorFile, $errorLine, $application = null);
        header("Location: /common/error/index.html/601");
    }

    public function _fnformatHandlerException($e) {
        $request = new \Phalcon\Http\Request();
        $errors = array(
            'info' => get_class($e) . '[' . $e->getCode() . ']: ',
            'message' => $e->getMessage(),
            'file' => $e->getFile(),
            'line' => $e->getLine(),
            'extraInfo' => $request->getClientAddress(),
            'trace' => "\n" . $e->getTraceAsString() . "\n"
        );
        $string = str_replace(" ", "", str_replace("'", "", substr($e->getMessage(), 0, 100)));
        $uniqueError = $e->getCode() . $e->getLine() . $string;
        $json_errors = json_encode($errors);
        $this->_fnlogger($json_errors, $uniqueError);
    }

    public function _fnformatHandlerError($errorCode, $errorMessage, $errorFile, $errorLine, $errcontext = null) {
        $request = new \Phalcon\Http\Request();
        $errors = array(
            'info' => $errorCode,
            'message' => $errorMessage,
            'file' => $errorFile,
            'line' => $errorLine,
            'extraInfo' => $request->getClientAddress(),
            'trace' => "\n" . $errcontext . "\n"
        );
        $string = str_replace(" ", "", str_replace("'", "", substr($errorMessage, 0, 100)));
        $uniqueError = $errorCode . $errorLine . $string;
        $json_errors = json_encode($errors);
        $this->_fnlogger($json_errors, $uniqueError);
    }

    public function _fnlogger($json_errors, $uniqueError) {
        $logger = DI::getDefault()->getLogger();
        $logger->error($json_errors, array($uniqueError));
        $logger->close();
    }

public function handleException($e) {
        if (ob_get_level() > 0) {
            ob_end_clean();
        }
        $this->_fnformatHandlerException($e);
        header("Location: /common/error/index.html/600");
    }

    public function handleError($errorCode, $errorMessage, $errorFile, $errorLine, $application = null) {
        if (ob_get_level() > 0) {
            ob_end_clean();
        }
        $this->_fnformatHandlerError($errorCode, $errorMessage, $errorFile, $errorLine, $application = null);
        header("Location: /common/error/index.html/601");
    }

    public function _fnformatHandlerException($e) {
        $request = new \Phalcon\Http\Request();
        $errors = array(
            'info' => get_class($e) . '[' . $e->getCode() . ']: ',
            'message' => $e->getMessage(),
            'file' => $e->getFile(),
            'line' => $e->getLine(),
            'extraInfo' => $request->getClientAddress(),
            'trace' => "\n" . $e->getTraceAsString() . "\n"
        );
        $string = str_replace(" ", "", str_replace("'", "", substr($e->getMessage(), 0, 100)));
        $uniqueError = $e->getCode() . $e->getLine() . $string;
        $json_errors = json_encode($errors);
        $this->_fnlogger($json_errors, $uniqueError);
    }

    public function _fnformatHandlerError($errorCode, $errorMessage, $errorFile, $errorLine, $errcontext = null) {
        $request = new \Phalcon\Http\Request();
        $errors = array(
            'info' => $errorCode,
            'message' => $errorMessage,
            'file' => $errorFile,
            'line' => $errorLine,
            'extraInfo' => $request->getClientAddress(),
            'trace' => "\n" . $errcontext . "\n"
        );
        $string = str_replace(" ", "", str_replace("'", "", substr($errorMessage, 0, 100)));
        $uniqueError = $errorCode . $errorLine . $string;
        $json_errors = json_encode($errors);
        $this->_fnlogger($json_errors, $uniqueError);
    }

    public function _fnlogger($json_errors, $uniqueError) {
        $logger = DI::getDefault()->getLogger();
        $logger->error($json_errors, array($uniqueError));
        $logger->close();
    }

this is my own hander error and exception :)