Skip to content

Instantly share code, notes, and snippets.

@jlamim
Last active November 14, 2016 23:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlamim/acdcd10618d3fe69980e9ae37b4f93ac to your computer and use it in GitHub Desktop.
Save jlamim/acdcd10618d3fe69980e9ae37b4f93ac to your computer and use it in GitHub Desktop.
Criando um restserver com CodeIgniter - index_post
<?php
/*
* Essa função vai responder pela rota /api/usuarios sob o método POST
*/
public function index_post()
{
// recupera os dados informado no formulário
$usuario = $this->post();
// verifica se a foto foi selecionada e faz o processamento
if (isset($_FILES['avatar'])) {
$upload = $this->UploadImage('avatar');
// se ocorreu algum erro no upload, retorna a mensagem de erro
// em caso de sucesso, armazena o path na variável $usuario
if ($upload['error']) {
$response['message'] = $upload['error'];
$this->response($response, REST_Controller::HTTP_BAD_REQUEST);
} else {
$usuario['avatar'] = $upload['upload_data']['file_name'];
}
}
// processa o insert no banco de dados
$insert = $this->UsuariosMDL->Insert($usuario);
// define a mensagem do processamento
$response['message'] = $insert['message'];
// verifica o status do insert para retornar o cabeçalho corretamente
// e a mensagem
if ($insert['status']) {
$this->response($response, REST_Controller::HTTP_OK);
} else {
$this->response($response, REST_Controller::HTTP_BAD_REQUEST);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment