Skip to content

Instantly share code, notes, and snippets.

@tamboer
Last active January 3, 2016 11:29
Show Gist options
  • Select an option

  • Save tamboer/8456463 to your computer and use it in GitHub Desktop.

Select an option

Save tamboer/8456463 to your computer and use it in GitHub Desktop.
ZF CRUD controller
<?php
class Controller_Base_Crud extends Zend_Controller_Action {
/**
* An instance of the corresponding Service layer
* @var object
*/
protected $_service = null;
/**
* Instantiate the Service class which corresponds
* to the current (calling) controller subclass
*
* @return object provides fluent interface
*
*/
protected function _setService() {
$serviceClassName = 'Service_' . substr(get_class($this), 0, -10);
$this->_service = new $serviceClassName();
return $this;
}
/**
* Get the Service class which corresponds
* to the current (calling) controller subclass
*
* @return object an instance of the corresponding service class
*
*/
protected function _getService() {
if ($this->_service == null) {
$this->_setService();
}
return $this->_service;
}
/**
* The index page shows a jQuery DataTables table which fetches
* its contents with AJAX, so no initialization is needed here
*
*
*/
public function indexAction() {
}
/**
* View a single record's details by an ID given in the URL
*
*
*/
public function viewAction() {
$controllerName = $this->getRequest()->getControllerName();
$recordId = $this->getRequest()->getParam('record-id');
$record = $this->_getService()->fetchOneBy(array('id' => $recordId), array('hydrate' => true));
if (!$record) {
$this->_helper->flashMessenger->setNameSpace('error')->addMessage('The selected record does not exist.');
$this->_helper->getHelper('Redirector')->gotoRoute(array(), $this->_controllerToRouteName($controllerName), TRUE);
}
$this->view->navigation()->findOneBy('controller', $controllerName)->setActive(true);
$recordName = lcfirst(implode('', array_map('ucfirst', (array) explode('-', $controllerName))));
$this->view->$recordName = $record;
}
/**
* Add one record
*
*
*/
public function addAction() {
$controllerName = $this->getRequest()->getControllerName();
$form = $this->_getService()->getForm('add');
if ($this->getRequest()->isPost()) {
$params = $this->getRequest()->getParams();
if ($form->isValid($params)) {
$addRecord = $this->_getService()->add($params);
if ($addRecord) {
$this->_helper->flashMessenger->setNameSpace('success')->addMessage('Record added.');
if ($this->getRequest()->getParam('submitAnother', FALSE)) {
$this->_helper->getHelper('Redirector')->gotoRoute(array('action' => 'add'), $this->_controllerToRouteName($controllerName), FALSE);
}
$this->_helper->getHelper('Redirector')->gotoRoute(array('action' => 'index'), $this->_controllerToRouteName($controllerName), FALSE);
} else {
$this->_helper->flashMessenger->setNameSpace('error')->addMessage('Record could not be added.');
}
} else {
$this->_helper->flashMessenger->setNameSpace('error')->addMessage('Please complete the form errors.');
}
}
$this->view->navigation()->findOneBy('controller', $controllerName)->setActive(true);
$this->view->form = $form;
}
/**
* Edit one record based on an ID given in the URL
*
*
*/
public function editAction() {
$controllerName = $this->getRequest()->getControllerName();
$record = $this->_getService()->fetchOneBy(array('id' => $this->getRequest()->getParam('record-id', FALSE)), array('hydrate' => true));
if (!$record) {
$this->_helper->flashMessenger->setNameSpace('error')->addMessage('The selected record does not exist.');
$this->_helper->getHelper('Redirector')->gotoRoute(array(), $this->_controllerToRouteName($controllerName), TRUE);
}
$form = $this->_getService()->getForm('edit', $record);
if ($this->getrequest()->isPost()) {
$params = $this->getRequest()->getParams();
if ($form->isValid($params)) {
$editRecord = $this->_getService()->edit($record['id'], $params, TRUE);
if ($editRecord) {
$this->_helper->flashMessenger->setNameSpace('success')->addMessage('Changes saved.');
$this->_helper->getHelper('Redirector')->gotoRoute(array('action' => 'index'), $this->_controllerToRouteName($controllerName), FALSE);
} else {
$form->populate($params);
$this->_helper->flashMessenger->setNameSpace('error')->addMessage('Changes could not be saved.');
}
} else {
$form->populate($params);
$this->_helper->flashMessenger->setNameSpace('error')->addMessage('Please complete the form errors.');
}
} else {
$form->populate($record);
}
$this->view->navigation()->findOneBy('controller', $controllerName)->setActive(true);
$recordName = lcfirst(implode('', array_map('ucfirst', (array) explode('-', $controllerName))));
$this->view->$recordName = $record;
$this->view->form = $form;
}
protected function _controllerToRouteName($controllerName) {
return implode('', array_map('ucfirst', explode('-', $controllerName)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment