Wow, really? PHP 7.2.1, Phalcon 3.3.0. Can I know yours?
index.php
namespace Notes;
use Phalcon\Config;
use Phalcon\Db\Adapter\Pdo\Sqlite;
use Phalcon\Mvc\Micro;
use Notes\Middleware\CorsMiddleware;
use Notes\Middleware\PostMiddleware;
use Notes\Middleware\AuthenticationMiddleware;
$app = new Micro();
require __DIR__ . '/../app/config/collections.php';
// ... Middleware, autoloader, config, etc...
$app->notFound(
function () use ($app) {
$app->response->setStatusCode(404, 'Not Found');
$app->response->sendHeaders();
$app->response->setContent('Not Found');
$app->response->send();
}
);
$app->error(
function ($exception) {
echo json_encode(
[
'code' => $exception->getCode(),
'status' => 'error',
'message' => $exception->getMessage(),
]
);
}
);
$app->handle();
collections.php
use Phalcon\Mvc\Micro\Collection;
$indexCollection = new Collection();
$indexCollection->setHandler('Notes\Controllers\IndexController', true);
$indexCollection->get('/', 'index');
$app->mount($indexCollection);
$userCollection = new Collection();
$userCollection->setHandler('Notes\Controllers\UserController', true);
$userCollection->post('/login', 'login');
$app->mount($userCollection);
UserController.php
namespace Notes\Controllers;
use Notes\Controllers\ControllerBase;
use Notes\Models\Users;
class UserController extends ControllerBase
{
public function login()
{
if (!isset($this->request->body->email) or !isset($this->request->body->password)) {
throw new \Exception('Unspecified email or password.', 400);
}
$user = Users::findFirstByEmail($this->request->body->email);
if (!$user) {
throw new \Exception('Invalid email or password.', 401);
}
throw new \Exception('abc', 200);
// ...
}
}