Skip to content

Instantly share code, notes, and snippets.

@juriansluiman
Last active December 16, 2015 17:49
Show Gist options
  • Save juriansluiman/5472787 to your computer and use it in GitHub Desktop.
Save juriansluiman/5472787 to your computer and use it in GitHub Desktop.
Standard CRUD methods in Controllers / Services / Repositories for Zend Framework 2
<?php
namespace Foo\Controller;
use Foo\Repository\BarInterface as Repository;
use Foo\Form\BarInterface as Form;
use Foo\Service\BarInterface as Service;
use Foo\Options\ModuleOptions;
use Zend\Mvc\Controller\AbstractActionController;
class BarController extends AbstractActionController
{
/**
* @var Repository
*/
protected $repository;
/**
* @var Service
*/
protected $service;
/**
* @var Form
*/
protected $form;
/**
* @var ModuleOptions
*/
protected $options;
public function __construct(Repository $repository, Service $service, Form $form, ModuleOptions $options = null)
{
$this->repository = $repository;
$this->service = $service;
$this->form = $form;
if (null !== $options) {
$this->options = $options;
}
}
public function getService()
{
return $this->service;
}
public function getRepository()
{
return $this->repository;
}
public function getForm()
{
return $this->form;
}
public function getOptions()
{
if (null === $this->options) {
$this->options = new ModuleOptions;
}
return $this->options;
}
public function indexAction()
{
$bars = $this->getRepository()->findAll();
return array(
'bars' => $bars,
);
}
public function viewAction()
{
$bar = $this->getBar();
return array(
'bar' => $bar,
);
}
public function createAction()
{
$bar = $this->getBar(true);
$form = $this->getForm();
$form->bind($bar);
if ($this->getRequest()->isPost()) {
$data = $this->getRequest()->getPost();
$form->setData($data);
if ($form->isValid()) {
// Bar is populated with form data
$this->getService()->create($bar);
return $this->redirect()->toRoute('bar/view', array(
'bar' => $bar->getId(),
));
}
}
return array(
'form' => $form,
);
}
public function updateAction()
{
$bar = $this->getBar();
$form = $this->getForm();
$form->bind($bar);
if ($this->getRequest()->isPost()) {
$data = $this->getRequest()->getPost();
$form->setData($data);
if ($form->isValid()) {
$this->getService()->update($bar);
return $this->redirect()->toRoute('bar/view', array(
'bar' => $bar->getId(),
));
}
}
return array(
'bar' => $bar,
'form' => $form,
);
}
public function deleteAction()
{
if (!$this->getRequest()->isPost()) {
$this->getRequest()->setStatusCode(404);
return;
}
$bar = $this->getBar();
$this->getService()->delete($bar);
return $this->redirect()->toRoute('bar');
}
protected function getBar($create = false)
{
if (true === $create) {
$class = $this->getOptions()->getBarEntityClassName();
$bar = new $class;
return $bar;
}
$id = $this->params('bar');
$bar = $this->getRepository()->find($id);
if (null === $bar) {
throw new Exception\BarNotFoundException(sprintf(
'Bar with id "%s" not found', $id
));
}
return $bar;
}
}
<?php
namespace Foo\Form;
use Zend\Form\FormInterface;
use Zend\InputFilter\InputFilterProviderInterface;
interface BarInterface implements FormInterface, InputFilterProviderInterface
{
}
<?php
namespace Foo\Repository;
use Foo\Repository\CriteriaInterface;
interface BarInterface
{
public function findAll();
public function find($id);
public function findBy(CriteriaInterface $criteria);
}
<?php
namespace Foo\Service;
use Foo\Entity\BarInterface;
use Foo\Service\BarInterface as BarServiceInterface;
use Doctrine\ORM\EntityManager;
use Zend\EventManager;
class Bar implements BarServiceInterface, EventManager\EventManagerAwareInterface
{
protected $entityManager;
protected $eventManager;
public function __construct(EntityManager $em)
{
$this->entityManager = $em;
}
public function getEntityManager()
{
return $this->entityManager;
}
public function create(BarInterface $bar)
{
$this->trigger(__FUNCTION__ . '.pre', array('bar' => $bar));
$this->getEntityManager()->persist($bar);
$this->getEntityManager()->flush();
$this->trigger(__FUNCTION__ . '.post', array('bar' => $bar));
}
public function update(BarInterface $bar)
{
$this->trigger(__FUNCTION__ . '.pre', array('bar' => $bar));
$this->getEntityManager()->flush();
$this->trigger(__FUNCTION__ . '.post', array('bar' => $bar));
}
public function delete(BarInterface $bar)
{
$this->trigger(__FUNCTION__ . '.pre', array('bar' => $bar));
$this->getEntityManager()->remove($bar);
$this->getEntityManager()->flush();
$this->trigger(__FUNCTION__ . '.post', array('bar' => $bar));
}
public function trigger($name, array $parameters = array())
{
$event = new EventManager\Event;
$event->setTarget($this);
$event->setName($name);
$event->setParams($parameters);
$this->getEventManager()->trigger($event);
}
/**
* Getter for eventManager
*
* @return mixed
*/
public function getEventManager()
{
if (null === $this->eventManager) {
$this->setEventManager(new EventManager\EventManager);
}
return $this->eventManager;
}
/**
* Setter for eventManager
*
* @param mixed $eventManager Value to set
* @return self
*/
public function setEventManager(EventManager\EventManagerInterface $eventManager)
{
$eventManager->setIdentifiers(array(
__CLASS__,
get_called_class(),
));
$this->eventManager = $eventManager;
return $this;
}
}
<?php
namespace Foo\Service;
use Foo\Entity\BarInterface;
interface BarInterface
{
public function create(BarInterface $bar);
public function update(BarInterface $bar);
public function delete(BarInterface $bar);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment