Last active
August 29, 2015 13:58
-
-
Save sergiopvilar/10000950 to your computer and use it in GitHub Desktop.
Criando uma API REST com CakePHP
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 echo json_encode($data); ?> |
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 | |
/** | |
* Created by PhpStorm. | |
* User: sergiovilar | |
* Date: 05/04/14 | |
* Time: 11:44 PM | |
*/ | |
class RestController extends AppController{ | |
public function index($id = null) { | |
switch($this->request->method()){ | |
case 'GET': | |
if(!empty($id)){ | |
$this->view($id); | |
}else{ | |
$this->listAll(); | |
} | |
break; | |
case 'POST': | |
if(!empty($id)){ | |
$this->add(); | |
}else{ | |
$this->update($id); | |
} | |
break; | |
case 'PUT': | |
$this->update($id); | |
break; | |
case 'DELETE': | |
$this->delete($id); | |
break; | |
} | |
} | |
// Métodos privados | |
private function renderJSON(){ | |
$this->layout = null; | |
$this->set('data', array('result' => $this->out)); | |
$this->render('/Elements/json'); | |
} | |
private function returnError($error){ | |
$this->layout = null; | |
$this->set('data', array('error' => $error)); | |
$this->render('/Elements/json'); | |
} | |
private function returnSuccess($msg){ | |
$this->layout = null; | |
$this->set('data', array('success' => $msg)); | |
$this->render('/Elements/json'); | |
} | |
protected function listAll(){ | |
$this->out = $this->{$this->model}->find('all'); | |
$this->renderJSON(); | |
} | |
private function view($id){ | |
$this->out = $this->{$this->model}->findById($id); | |
$this->renderJSON(); | |
} | |
protected function add() { | |
$this->{$this->model}->create(); | |
if ($this->{$this->model}->save($this->request->data)) { | |
$this->returnSuccess("Registro adicionado com sucesso"); | |
} else { | |
$this->returnError("Houve um problema ao adicionar o registro"); | |
} | |
} | |
protected function update($id) { | |
$this->{$this->model}->id = $id; | |
if ($this->{$this->model}->save($this->request->data)) { | |
$this->returnSuccess("Registro atualizado com sucesso"); | |
} else { | |
$this->returnError("Houve um problema ao atualizar o registro"); | |
} | |
} | |
protected function delete($id) { | |
if ($this->{$this->model}->delete($id)) { | |
$this->returnSuccess("Registro excluído com sucesso"); | |
} else { | |
$this->returnError("Houve um problema ao excluir o registro"); | |
} | |
} | |
} |
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 | |
App::uses('AppController', 'Controller'); | |
require_once(ROOT . DS . 'app' . DS . 'Controller' . DS . 'RestController.php'); | |
/** | |
* Sample Controller | |
* | |
*/ | |
class SampleController extends RestController { | |
public $model = 'Sample'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment