calling dispatcher from bootstrap

Hi everyone, I have a bootstrap_web.php file in my multi module application. which has try catch approach.

When my application throws an exception, the page shows the message but does not follow the theme and shows the message in a white page.

my code in catch section is:

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

Now, I want the catch section to set a flash message and redirect to the last page where exception occurred.

How can i do that?



67.7k
edited Oct '17

you need something like

$this->get("flashSession")->error(); // i dont remember the code to add error there exactly
$this->get("dispatcher")->redicrect();

i catch my exceptions at public/index.php , no idea if that is correct or not


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

    $application->main();
} catch (\CommonClasses\CapchaException $e){

    $msg = "whatever";

    $application->di->get("flashSession")->error($msg);
    header("Location: ". $di->get("request")->getHTTPReferer());
    exit;

} catch (...) {
    $request = new \Phalcon\Http\Request();

    if (true === $request->isAjax()){

        if ($application->di->get("config")->env !== "development"){

            echo json_encode([
                "success" => false,
                "message" => "ehhh"
            ]);
            http_response_code(405);
            exit;

        } else {

            echo json_encode([
                "success" => false,
                "message" => $e->getMessage() . $e->getTraceAsString()
            ]);
            http_response_code(405);
            exit;
        }

    }
}