Skip to content

Instantly share code, notes, and snippets.

@andersonfraga
Created February 22, 2011 17:16
Show Gist options
  • Save andersonfraga/839007 to your computer and use it in GitHub Desktop.
Save andersonfraga/839007 to your computer and use it in GitHub Desktop.
Controller para CRUD e com suporte à XML e JSON um tanto quanto minimalista...
<?php
class NoticiaConteudoController extends RestritoController {
function dependency() {
return Array(
'noticia' => '@NoticiaConteudoModel',
'autor' => '@AutorModel',
);
}
function respond_to() {
return Array(
'format' => Array('html', 'json', 'xml'),
'act_as' => Array(
'get_noticia' => Array('json', 'xml'),
'get_detalhe' => Array('json', 'xml'),
),
);
}
function csrf_protect() {
return Array(
'add' => Array('get_noticia.json', 'get_detalhe.json'),
);
}
function cache_for() { // output cache
return Array(
'expires' => DateHelperThin::minutes_for_seconds(2),
'pages' => Array(
'only' => Array('noticia', 'adicionar'), // SOMENTE MÉTODOS 'GET' SERÃO VALIDADOS
),
);
}
//
// @GET /conteudo/noticia[/pg:1.html|.json|.xml]
function get_noticia($pg = 1) {
return Array(
'lista_noticia' => (RequestHelperThin::extension() != 'html')
? $this->noticia()->listarTodos()
: $this->noticia()->listarComPaginacao($pg),
);
}
//
// @GET /conteudo/noticia/adicionar.html
function get_adicionar() {
return Array(
'_response:view' => 'editar',
);
}
//
// @GET /noticia/buscar.html?q=blabla
function get_buscar($pg = 1) {
return Array(
'_response:view' => 'index',
'lista_noticia' => $this->noticia()->buscar(RequestHelperThin::data_get('q'), $pg),
);
}
//
// @GET /conteudo/noticia/detalhe/10.[html|json|xml]
function get_detalhe($id) {
return $this->noticia()->buscarPeloId($id);
}
//
// @GET /conteudo/noticia/editar/12.html
function get_editar($id) {
return $this->noticia()->buscarPeloId($id);
}
//
// @POST /conteudo/noticia/adicionar.html
function post_adicionar() {
return $this->noticia()->adicionar(RequestHelperThin::data_post('noticia'));
}
//
// @PUT /conteudo/noticia/editar/12.html
function put_editar($id) {
return $this->noticia()->editar($id, RequestHelperThin::data_post('noticia'));
}
//
// @DELETE /conteudo/noticia/excluir/12.html
function delete_excluir($id) {
return $this->noticia()->deletar($id);
}
//
// PARTIAL listagem de autores
function partial_lista_autores() {
return $this->autor()->listarTodos();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment