Skip to content

Instantly share code, notes, and snippets.

Created October 24, 2016 20:46
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 anonymous/0079c597edca3978ca40e6cc1967a073 to your computer and use it in GitHub Desktop.
Save anonymous/0079c597edca3978ca40e6cc1967a073 to your computer and use it in GitHub Desktop.
Série RestServer - Controller Usuarios
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH . '/libraries/REST_Controller.php';
class Usuarios extends REST_Controller {
function __construct()
{
parent::__construct();
$this->load->model('Usuarios_model','UsuariosMDL');
}
/*
* Essa função vai responder pela rota /api/usuarios sob o método GET
*/
public function index_get()
{
// Lista os usuários
$usuarios = $this->UsuariosMDL->GetAll('id, nome, email');
// verifica se existem usuários e faz o retorno da requisição
// usando os devidos cabeçalhos
if ($usuarios) {
$response['data'] = $usuarios;
$this->response($response, REST_Controller::HTTP_OK);
} else {
$this->response(null,REST_Controller::HTTP_NO_CONTENT);
}
}
/*
* 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();
// 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