Hello, I need some help. I am building an API with phalcon and it has given problem. I try to insert something but it adds nothing and error 500, see the code: I know there are some security issues, I'll fix later.
<?php
use Phalcon\Di\FactoryDefault;
use Phalcon\Http\Response;
use Phalcon\Mvc\Micro;
error_reporting(E_ALL);
define('BASE_PATH', dirname(__DIR__));
define('APP_PATH', BASE_PATH . '/app');
try {
/**
* The FactoryDefault Dependency Injector automatically registers the services that
* provide a full stack framework. These default services can be overidden with custom ones.
*/
$di = new FactoryDefault();
/**
* Include Services
*/
include APP_PATH . '/config/services.php';
/**
* Get config service for use in inline setup below
*/
$config = $di->getConfig();
/**
* Include Autoloader
*/
include APP_PATH . '/config/loader.php';
/**
* Starting the application
* Assign service locator to the application
*/
$app = new Micro($di);
$app->post(
"/api/membros",
function () use ($app) {
$phql = "SELECT * FROM Membros";
$dados = $app->modelsManager->executeQuery($phql);
$data = [];
foreach ($dados as $dado) {
$data[] = [
"id" => $dado->id,
"id_usuario" => $dado->id_usuario,
"nome" => $dado->nome,
"email" => $dado->email,
"telefone" => $dado->telefone,
"cpf" => $dado->cpf,
"rg" => $dado->rg,
"endereco" => $dado->endereco,
"profissao" => $dado->profissao,
];
}
echo json_encode($data);
}
);
$app->get(
"/api/membros/{id:[0-9]+}",
function ($id) use ($app) {
$phql = "SELECT * FROM Membros WHERE Membros.id = " . $id;
$dados = $app->modelsManager->executeQuery($phql);
$data = [];
foreach ($dados as $dado) {
$data[] = [
"id" => $dado->id,
"id_usuario" => $dado->id_usuario,
"nome" => $dado->nome,
"email" => $dado->email,
"telefone" => $dado->telefone,
"cpf" => $dado->cpf,
"rg" => $dado->rg,
"endereco" => $dado->endereco,
"profissao" => $dado->profissao,
];
}
echo json_encode($data);
}
);
$app->post(
"/api/membros",
function () use ($app) {
$insert = [
'id_usuario' => $app->request->getPost('id_usuario'),
'nome' => $app->request->getPost('nome'),
'email' => $app->request->getPost('email'),
'telefone' => $app->request->getPost('telefone'),
'cpf' => $app->request->getPost('cpf'),
'rg' => $app->request->getPost('rg'),
'endereco' => $app->request->getPost('endereco'),
'profissao' => $app->request->getPost('profissao'),
];
$phql = "INSERT INTO Membros (id_usuario, nome, email, telefone, cpf, rg,endereco, profissao) VALUES ('" . $insert['id_usuario'] . "','" . $insert['nome'] . "','" . $insert['email'] . "','" . $insert['telefone'] . "','" . $insert['cpf'] . "','" . $insert['rg'] . "','" . $insert['endereco'] . "','" . $insert['profissao'] . "')";
echo $phql;
$status = $app->modelsManager->executeQuery($phql);
$response = new Response();
if ($status->success() === true) {
$response->setStatusCode(201, "Adicionado com Sucesso");
$dados = Membros::findFirstBynome($insert['nome']);
$response->setJsonContent(
[
'status' => "OK",
'data' => $dados,
]
);
} else {
$response->setStatusCode(409, "Conflito");
$erro = [];
foreach ($status->getMessage() as $msg) {
$erros[] = $msg->getMessage();
}
$response->setJsonContent(
[
'status' => "Erro",
'messages' => $erros,
]
);
}
return $response;
}
);
$app->notFound(
function () use ($app) {
echo "Desculpe, não foi possivel atender sua solicatação";
}
);
/**
* Handle the request
*/
$app->handle();
} catch (\Exception $e) {
echo $e->getMessage() . '<br>';
echo '<pre>' . $e->getTraceAsString() . '</pre>';
}