Last active
November 14, 2016 23:20
-
-
Save jlamim/acdcd10618d3fe69980e9ae37b4f93ac to your computer and use it in GitHub Desktop.
Criando um restserver com CodeIgniter - index_post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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