Created
January 12, 2013 15:08
-
-
Save eminetto/4518429 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
namespace Application\Controller; | |
use Zend\View\Model\ViewModel; | |
use Core\Controller\ActionController; | |
use Zend\Paginator\Paginator; | |
use Zend\Paginator\Adapter\DbSelect as PaginatorDbSelectAdapter; | |
use Symfony\Component\Serializer\Serializer; | |
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer; | |
use Symfony\Component\Serializer\Encoder\JsonEncoder; | |
use Symfony\Component\Serializer\Encoder\XmlEncoder; | |
/** | |
* Controlador que gerencia os posts | |
* | |
* @category Application | |
* @package Controller | |
* @author Elton Minetto <[email protected]> | |
*/ | |
class IndexController extends ActionController | |
{ | |
/** | |
* Mostra os posts cadastrados | |
* @return void | |
*/ | |
public function indexAction() | |
{ | |
$post = $this->getTable('Application\Model\Post'); | |
$sql = $post->getSql(); | |
$select = $sql->select(); | |
$paginatorAdapter = new PaginatorDbSelectAdapter($select, $sql); | |
$paginator = new Paginator($paginatorAdapter); | |
$paginator->setCurrentPageNumber($this->params()->fromRoute('page')); | |
$paginator->setItemCountPerPage(10); | |
return new ViewModel(array( | |
'posts' => $paginator | |
)); | |
} | |
/** | |
* Retorna os comentários de um post | |
* @return Zend\Http\Response | |
*/ | |
public function commentsAction() | |
{ | |
$id = (int) $this->params()->fromRoute('id', 0); | |
if ($id == 0) { | |
throw new \Exception("Código obrigatório"); | |
} | |
$format = $this->params()->fromRoute('format', 'json'); | |
$where = array('post_id' => $id); | |
$comments = $this->getTable('Application\Model\Comment') | |
->fetchAll(null, $where) | |
->toArray(); | |
$serializer = new Serializer( | |
array(new GetSetMethodNormalizer()), | |
array( | |
'xml' => new XmlEncoder(), | |
'json' => new JsonEncoder() | |
) | |
); | |
if ($format == 'json') { | |
$content = $serializer->serialize($comments, 'json'); | |
} | |
if ($format == 'xml') { | |
$content = $serializer->serialize($comments, 'xml'); | |
} | |
$response = $this->getResponse(); | |
$response->setStatusCode(200); | |
$response->setContent($content); | |
$response->getHeaders()->addHeaderLine('Content-Type', 'application/' . $format); | |
return $response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment