That shouldn't be neccecary (handling wildcard pattern routes).
// Not found handler
$app->notFound(function () { ErrorHandler::stopMiddleware(404); });
My error handler class / static method stopMiddleware looks like this:
static final function stopMiddleware(int $status = 401, string $msg = 'STOP RUN.', $code = 0x0a){
//Resolve last used DI from static context
static::$di = \Phalcon\Di::getDefault();
//resolve shared service from static context into local scope
$response = static::$di->getShared('response');
//set requested status code and response content (in my case for API I'm using JSON)
$response->setStatusCode($status);
$response->setJsonContent(['Status' => $status, 'Type' => 'ACCESS_DENIED', 'Error' => $msg, 'Code' => $code], JSON_NUMERIC_CHECK);
$response->setContentLength(strlen($response->getContent())); //Calculate Content-Length header
//flush output to the client
!$response->isSent() && $response->send();
//returning false will stop execution
return false;
}
Router should be configured like this:
$di->setShared('router', function (){
// Phalcon\Mvc\Router has a default behavior that provides a very simple routing that always expects a URI that matches the following pattern: /:controller/:action/:params
$router = new \Phalcon\Mvc\Router(false); //Create the router without default routes
//we're using Front Page Controller pattern in relation from nginx -> Phalcon
$router->setUriSource($router::URI_SOURCE_SERVER_REQUEST_URI); // Use $_SERVER['REQUEST_URI']
//Set whether router must remove the extra slashes in the handled routes
$router->removeExtraSlashes(true);
return $router;
});
P.S. It is recommended to handle your routes via MicroCollection.