Okay this is the second time I've rewritten this, so I'm going to keep it short. I'm writing an API for my website, and I need to handle 404 errors and output them as JSON, rather than rendering a HTML view. Does anyone know how I could do this? Thank you for your time.
My handlers are defined like so:
\Controllers\ErrorController::notFound(); // This should output HTML
\Controllers\Api\ErrorController::notFound(); // This should output JSON
My routing is defined like so:
$router = new \Phalcon\Mvc\Router(false);
$router->setDefaultNamespace('Controllers');
$router->notFound("Error::notFound");
$router->mount(new \Routes\Api());
My API routes class is defined like so:
namespace Routes;
use Phalcon\Mvc\Router\Group;
class Api extends Group
{
public function initialize()
{
// Prefixes for the standard routes
$this->setPrefix('/api');
$this->setPaths(["namespace" => 'Controllers\Api']);
// How do I handle a custom 404 error here?
// This doesn't seem to be possible
$this->notFound("Error::notFound");
}
}