Skip to content

Instantly share code, notes, and snippets.

@hugoleodev
Created July 11, 2012 12:57
Show Gist options
  • Save hugoleodev/3090232 to your computer and use it in GitHub Desktop.
Save hugoleodev/3090232 to your computer and use it in GitHub Desktop.
Esquema de controllers + CRUD + Service Layer
<?php
namespace Core\Controller;
class Action extends \Zend_Controller_Action
{
/**
* EntityManager retornado
* @var \Doctrine\ORM\EntityManager
*/
protected $em;
public function init()
{
$this->em = $this->getEntityManager();
}
/**
* Retorna o container (configuracoes) do doctrine
*
* @return \Bisna\Doctrine\Container
*/
public function getDoctrineContainer()
{
return $this->getInvokeArg('bootstrap')->getResource('doctrine');
}
/**
* Retorna um entityManager. ùtil nos controller
*
* @param string $em nome do entitymanager
* @return type
*/
public function getEntityManager($em = null)
{
return $this->getDoctrineContainer()->getEntityManager($em);
}
public function getDBALConnection($connName = null)
{
return $this->getDoctrineContainer()->getConnection($connName);
}
/**
*
* @return Bisna\Service\ServiceLocator
*/
public function getServiceLocator()
{
return $this->getInvokeArg('bootstrap')->getResource('serviceLocator');
}
}
<?php
use \Core\Controller\CrudServiceController;
class Admin_CrudController extends CrudServiceController
{
public $serviceName = null;
/**
* @return \Bisna\Service\Service
*/
public function getService()
{
$this->serviceName = $this->_getParam('service');
if ($this->serviceName == null) {
throw new \Exception('Unknow service');
}
return $this->getServiceLocator()->getService($this->serviceName);
}
}
<?php
namespace Core\Controller;
/*
* Workshop Doctrine 2 + ZF1
*
*/
use \Core\Controller\Action;
use \Core\Grid;
/**
* CategoriaController
*
* @author Administrator
*/
abstract class CrudServiceController extends Action
{
public $serviceName = null;
/**
* @return \Bisna\Service\Service
*/
public function getService()
{
if ($this->serviceName == null) {
throw new \Exception('Unknow service');
}
return $this->getServiceLocator()->getService($this->serviceName);
}
public function indexAction()
{
$grid = $this->getGrid();
$this->view->grid = $grid->render();
}
public function addAction()
{
$service = $this->getService();
$form = $this->getForm();
$request = $this->getRequest();
if ($request->isPost()) {
if ($form->isValid($request->getParams())) {
$service->add($form->getValues());
$this->_helper->redirector('index');
} else {
$form->populate($request->getParams());
}
}
$this->view->form = $form;
}
public function editAction()
{
$id = $this->_getParam('id');
$service = $this->getService();
$entity = $service->find($id);
$form = $this->getForm();
$form = $this->prepareForm($form);
$request = $this->getRequest();
if ($entity == null) {
throw new \Exception('Registro não existe');
}
$form->setDefaultsFromEntity($entity);
if ($request->isPost()) {
if ($form->isValid($request->getParams())) {
$service->edit($id, $form->getValues());
$this->_helper->redirector('index');
} else {
$form->populate($request->getParams());
}
}
$this->view->form = $form;
}
public function deleteAction()
{
$id = $this->_getParam('id');
$this->getService()->delete($id);
$this->_helper->redirector('index');
}
/**
*
* @return \Zend_Form
*/
protected function getForm()
{
$formClass = '\Application\Form\\' . ucfirst($this->serviceName);
return new $formClass;
}
/**
*
* @return \Zend_Form
*/
protected function getGrid()
{
$formClass = '\Application\Grid\\' . ucfirst($this->serviceName);
return new $gridClass;
}
/**
*
* @return \Zend_Form
*/
public function prepareForm(\Zend_Form $form)
{
return $form;
}
}
http://app.local/admin/crud/index/service/user
http://app.local/admin/crud/edit/service/user
http://app.local/admin/crud/add/service/user
http://app.local/admin/crud/delete/service/user
http://app.local/admin/crud/index/service/foo
http://app.local/admin/crud/edit/service/foo
http://app.local/admin/crud/add/service/foo
http://app.local/admin/crud/delete/service/foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment