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

$app->before(return false;) not stopping route execution

this code 100% failes to stop route execurtion.


$app = new Micro($di);

$app->before(function () use ($app) {
    // Auth logic here. 
    return $isAuthed; // this is false when testing. 
});

$app->after(function() use ($app){
    // this code still executes. even though wer ereturned false in the before fucntion
    if (!$app->response->isSent()){
        $app->response->send();
    }
});

// the base response for the api
$app->get('/',function() use ($app){
    $app->response->setJsonContent('Welcome to the Api');
});

// not found response. 
$app->notFound(function () use ($app) {
    $app->response->setHeader("Content-Type", "application/json");
    $app->response->setStatusCode(404,'Not Found');
    $app->response->setJsonContent('Not Found');
    return $app->response;
});

// routes
require ( APP_PATH. '/config/v2routes.php' );
foreach($v2 as $route){
    $app->mount($route);
}

I think i am using the old api here. looking at the docs again it appears to have changed. rather a lot.

edited Sep '17

Yep that was it. The below code works for stopping as of 3.2


$eventsManager = new EventsManager();
$eventsManager->attach(
    'micro:beforeExecuteRoute',
    function (Event $event, $app) {
        // LOGIC
        return $isAuthed;
    }
);