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

throw new Exception with Phalcon 2.1 Multi module

Hi all, I using Phalcon 2.1 with multi module. I tried several times with "throw new Exception" but unsuccessfully. Please help me

I visit this action and get whitepapers I want the content error show in the file view module Home

1 In Action

throw new \Exception("Không tồn tại mẫu tin.");

2 File services.php. I think the problem in the services file

$di = new FactoryDefault();
$di->set('dispatcher', function () {
    // Create an EventsManager
    $eventsManager = new EventsManager();

    // Attach a listener
    $eventsManager->attach("dispatch:beforeException", function ($event, $dispatcher, $exception) {
        // Handle 404 exceptions
        if ($exception instanceof DispatchException) {
            $dispatcher->forward([
                'namespace' => 'App\Modules\Home\Controllers',
                'module' => 'home',
                'controller' => 'index',
                'action' => 'show404'
            ]);
            return false;
        }

        // Alternative way, controller or action doesn't exist
        switch ($exception->getCode()) {
            case Dispatcher::EXCEPTION_NO_DI:
                $msg = $exception->getMessage();
                if ($dispatcher->getControllerName() == 'admin') {
                    $dispatcher->forward([
                        'namespace' => 'App\Modules\Admin\Controllers',
                        'module' => 'admin',
                        'controller' => 'admin',
                        'action' => 'error',
                        'params' => ['msg' => $msg]
                    ]);
                }else{
                    $dispatcher->forward([
                        'namespace' => 'App\Modules\Home\Controllers',
                        'module' => 'home',
                        'controller' => 'index',
                        'action' => 'error',
                        'params' => ['msg' => $msg]
                    ]);
                }
                return false;
            case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
            case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                $dispatcher->forward(
                    array(
                        'controller' => 'index',
                        'action' => 'show404'
                    )
                );
                return false;
        }
    });

    $dispatcher = new MvcDispatcher();
    // Bind the EventsManager to the dispatcher
    $dispatcher->setEventsManager($eventsManager);
    return $dispatcher;
});

3 index.php

try{
} catch (\Exception $e) {
    echo $e->getMessage() . '<br>';
    echo '<pre>' . $e->getTraceAsString() . '</pre>';
}


85.5k
edited Jul '16

bah i suck at those...

mine is looking like this and its working.

if (APPLICATION_ENV === "development"){

    try {
        $application = new \Shop\Application\Application(new FactoryDefault());

        $application->main();

    } catch (\Phalcon\Exception $e) {
        echo "<body style='background-color: black; color: red;'><pre>";
        print_r('A Phalcon\Exception occurred: ' . $e->getMessage() . $e->getTraceAsString());
        echo "</pre>";
    } catch (\PDOException $e) {
        echo "<pre>";
        print_r('A PDOException occurred: ' . $e->getMessage() . $e->getTraceAsString());
        echo "</pre>";
    } catch (\Exception $e) {
        echo "<pre>";
        print_r('An Exception occurred: '. $e->getMessage() . $e->getTraceAsString());
        echo "</pre>";
    }
}

//aplication main function definition

    /**
     * Handles the request and echoes its content to the output stream.
     */
    public function main()
    {
        echo $this->handle()->getContent();
    }