Snippets úteis do Zend Framework
Compartilho aqui alguns snippets e comandos úteis do Zend Framework, para referência futura e quem sabe ajudar alguém que precise.
Retornar o código de resposta 404 (Not found).
<?php
$this->_response->clearBody()->clearHeaders()->setHttpResponseCode(404)->sendResponse();
Redirecionar para URL com âncora (sharp/cerquilha/jogo da velha)
<?php
$opt = array('module' => 'admin', 'controller' => 'galeria', 'action' => 'editar', 'id' => 1); $url = $this->_helper->getHelper('Url')->url($opt); $url .= "#descricao"; $this->_helper->redirector->gotoUrl($url);
Trocar o layout no controller
<?php
$this->_helper->layout->setLayout('servicos');
Timestamp no formato de banco de dados
<?php
Zend_Date::now()->toString('yyyy-MM-dd HH:mm:ss');
Desabilitar layout
<?php
$this->_helper->layout()->disableLayout();
Desabilitar renderização da view
<?php
$this->_helper->viewRenderer->setNoRender(true);
Debugar consultas no banco de dados
<?php
$db = Zend_Db_Table::getDefaultAdapter(); $profiler = $db->getProfiler()->setEnabled(true); print_r($profiler->getLastQueryProfile());
Dados do usuário logado (caso esteja usando Zend_Auth)
<?php
Zend_Auth::getInstance()->getIdentity();
Utilizando transações:
<?php
$db = Zend_Db_Table::getDefaultAdapter();
try{
$db->beginTransaction();
// query
$db->commit();
}
catch (Exception $e){
$db->rollBack();
}