我使用Zend Framework,在我的应用程序中,admin主要用于CRUD操作。 我不使用zend表单,如果可能的话我想保持这种方式,但我认为我的代码根本不好,并且它可以改进分配所以我很好地问你如何更好地组织这个类(因为我所有的班级看起来都差不多了)......
以下是代码:
答案 0 :(得分:1)
Sample Admin CRUD Controller (for hierarchical categories)
class StoreAdmin_CategoryController extends Zend_Controller_Action
{
protected $_flashMessenger = null;
public function init()
{
$this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');
$this->initView();
}
public function indexAction()
{
$this->_forward('list');
}
public function listAction()
{
$table_category = new Store_Model_DbTable_Category();
$obj_select = $table_category->build_select(array(), array('order_by' => 'ASC'));
$arr_categories = $table_category->fetch_all_objects($obj_select);
$table_category = new Store_Model_DbTable_Category();
$hierarchy = $table_category->get_hierarchy(NULL);
$this->view->assign('arr_categories', $arr_categories);
$this->view->assign('hierarchy', $hierarchy);
}
public function createAction()
{
$form = new StoreAdmin_Form_Category();
if($this->getRequest()->isPost())
{
if($form->isValid($this->getRequest()->getPost()))
{
$table_category = new Store_Model_DbTable_Category();
$category = $table_category->get();
$category->set_name($form->getValue('name'));
$category->set_slug($form->getValue('slug'));
$category->set_description($form->getValue('description'));
$category->set_order_by($form->getValue('order_by'));
$category->set_id_parent($form->getValue('id_parent'));
$category->set_date_created(time());
$category->set_date_modified(time());
$table_category->save($category);
$this->_helper->FlashMessenger(array('success' => sprintf("Category: %s was successfully created!", $category->get_name())));
$this->_redirect('/store-admin/category/list');
}
else
{
$this->_helper->FlashMessenger(array('error' => "Errors! Correct the errors in the form below"));
}
}
$this->view->assign('form_category', $form);
}
public function updateAction()
{
$id_category = $this->getRequest()->getParam('id_category');
if(empty($id_category))
{
throw new Zend_Exception('Error: Category ID Missing from request');
}
$form = new StoreAdmin_Form_Category();
$form->removeOption($id_category);
$table_category = new Store_Model_DbTable_Category();
$category = $table_category->get($id_category);
if($this->getRequest()->isPost())
{
if($form->isValid($this->getRequest()->getPost()))
{
$category->set_name($form->getValue('name'));
$category->set_slug($form->getValue('slug'));
$category->set_description($form->getValue('description'));
$category->set_order_by($form->getValue('order_by'));
$category->set_id_parent($form->getValue('id_parent') ? $form->getValue('id_parent') : NULL);
$category->set_date_modified(time());
$table_category->save($category);
$this->_helper->FlashMessenger(array('success' => sprintf("Category: %s was successfully updated!", $category->get_name())));
$this->_redirect('/store-admin/category/list');
}
else
{
$this->_helper->FlashMessenger(array('error' => "Errors! Correct the errors in the form below"));
}
}
else
{
$form->populate($category->to_array());
}
$this->view->assign('form_category', $form);
$this->view->assign('category', $category);
}
public function deleteAction()
{
$id_category = $this->getRequest()->getParam('id_category');
if(empty($id_category))
{
throw new Zend_Exception('Error: Category ID Missing from request');
}
$table_category = new Store_Model_DbTable_Category();
$category = $table_category->get($id_category);
$table_category->destroy($category);
$this->_helper->FlashMessenger(array('success' => "Category was successfully deleted!"));
$this->_redirect('/store-admin/category/list');
}
}
class StoreAdmin_CategoryController extends Zend_Controller_Action
{
protected $_flashMessenger = null;
public function init()
{
$this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');
$this->initView();
}
public function indexAction()
{
$this->_forward('list');
}
public function listAction()
{
$table_category = new Store_Model_DbTable_Category();
$obj_select = $table_category->build_select(array(), array('order_by' => 'ASC'));
$arr_categories = $table_category->fetch_all_objects($obj_select);
$table_category = new Store_Model_DbTable_Category();
$hierarchy = $table_category->get_hierarchy(NULL);
$this->view->assign('arr_categories', $arr_categories);
$this->view->assign('hierarchy', $hierarchy);
}
public function createAction()
{
$form = new StoreAdmin_Form_Category();
if($this->getRequest()->isPost())
{
if($form->isValid($this->getRequest()->getPost()))
{
$table_category = new Store_Model_DbTable_Category();
$category = $table_category->get();
$category->set_name($form->getValue('name'));
$category->set_slug($form->getValue('slug'));
$category->set_description($form->getValue('description'));
$category->set_order_by($form->getValue('order_by'));
$category->set_id_parent($form->getValue('id_parent'));
$category->set_date_created(time());
$category->set_date_modified(time());
$table_category->save($category);
$this->_helper->FlashMessenger(array('success' => sprintf("Category: %s was successfully created!", $category->get_name())));
$this->_redirect('/store-admin/category/list');
}
else
{
$this->_helper->FlashMessenger(array('error' => "Errors! Correct the errors in the form below"));
}
}
$this->view->assign('form_category', $form);
}
public function updateAction()
{
$id_category = $this->getRequest()->getParam('id_category');
if(empty($id_category))
{
throw new Zend_Exception('Error: Category ID Missing from request');
}
$form = new StoreAdmin_Form_Category();
$form->removeOption($id_category);
$table_category = new Store_Model_DbTable_Category();
$category = $table_category->get($id_category);
if($this->getRequest()->isPost())
{
if($form->isValid($this->getRequest()->getPost()))
{
$category->set_name($form->getValue('name'));
$category->set_slug($form->getValue('slug'));
$category->set_description($form->getValue('description'));
$category->set_order_by($form->getValue('order_by'));
$category->set_id_parent($form->getValue('id_parent') ? $form->getValue('id_parent') : NULL);
$category->set_date_modified(time());
$table_category->save($category);
$this->_helper->FlashMessenger(array('success' => sprintf("Category: %s was successfully updated!", $category->get_name())));
$this->_redirect('/store-admin/category/list');
}
else
{
$this->_helper->FlashMessenger(array('error' => "Errors! Correct the errors in the form below"));
}
}
else
{
$form->populate($category->to_array());
}
$this->view->assign('form_category', $form);
$this->view->assign('category', $category);
}
public function deleteAction()
{
$id_category = $this->getRequest()->getParam('id_category');
if(empty($id_category))
{
throw new Zend_Exception('Error: Category ID Missing from request');
}
$table_category = new Store_Model_DbTable_Category();
$category = $table_category->get($id_category);
$table_category->destroy($category);
$this->_helper->FlashMessenger(array('success' => "Category was successfully deleted!"));
$this->_redirect('/store-admin/category/list');
}
}
Sample create view
<div class="span-24">
<a href="/store-admin/category/list">List</a>
<form action="<?php echo $this->form_category->getAction(); ?>" method="<?php echo $this->form_category->getMethod(); ?>">
<h2>Create a Category</h2>
<p>
<?php echo $this->form_category->getElement('name')->renderLabel(); ?>
<br>
<?php echo $this->form_category->getElement('name')->renderViewHelper(); ?>
<?php echo (NULL != ($errors = $this->form_category->getElement('name')->getMessages()) ? $this->formErrors($errors) : ''); ?>
</p>
<p>
<?php echo $this->form_category->getElement('slug')->renderLabel(); ?>
<br>
<?php echo $this->form_category->getElement('slug')->renderViewHelper(); ?>
<?php echo (NULL != ($errors = $this->form_category->getElement('slug')->getMessages()) ? $this->formErrors($errors) : ''); ?>
</p>
<p>
<?php echo $this->form_category->getElement('id_parent')->renderLabel(); ?>
<br>
<?php echo $this->form_category->getElement('id_parent')->renderViewHelper(); ?>
<?php echo (NULL != ($errors = $this->form_category->getElement('id_parent')->getMessages()) ? $this->formErrors($errors) : ''); ?>
</p>
<p>
<?php echo $this->form_category->getElement('description')->renderLabel(); ?>
<br>
<?php echo $this->form_category->getElement('description')->renderViewHelper(); ?>
<?php echo (NULL != ($errors = $this->form_category->getElement('description')->getMessages()) ? $this->formErrors($errors) : ''); ?>
</p>
<p>
<?php echo $this->form_category->getElement('order_by')->renderLabel(); ?>
<br>
<?php echo $this->form_category->getElement('order_by')->renderViewHelper(); ?>
<?php echo (NULL != ($errors = $this->form_category->getElement('order_by')->getMessages()) ? $this->formErrors($errors) : ''); ?>
</p>
<p>
<?php echo $this->form_category->getElement('submit_category')->renderViewHelper(); ?>
</p>
</form>
</div>
<script type="text/javascript">
$(document).ready(
function()
{
$('#name').change( slugify ).keyup( slugify );
}
);
function slugify()
{
text = $(this).val().toLowerCase();
text = text.replace(/[^-a-zA-Z0-9,&\s]+/ig, '');
text = text.replace(/-/gi, "_");
text = text.replace(/\s/gi, "-");
$('#slug').val(text);
}
</script>
<div class="span-24">
<a href="/store-admin/category/list">List</a>
<form action="<?php echo $this->form_category->getAction(); ?>" method="<?php echo $this->form_category->getMethod(); ?>">
<h2>Create a Category</h2>
<p>
<?php echo $this->form_category->getElement('name')->renderLabel(); ?>
<br>
<?php echo $this->form_category->getElement('name')->renderViewHelper(); ?>
<?php echo (NULL != ($errors = $this->form_category->getElement('name')->getMessages()) ? $this->formErrors($errors) : ''); ?>
</p>
<p>
<?php echo $this->form_category->getElement('slug')->renderLabel(); ?>
<br>
<?php echo $this->form_category->getElement('slug')->renderViewHelper(); ?>
<?php echo (NULL != ($errors = $this->form_category->getElement('slug')->getMessages()) ? $this->formErrors($errors) : ''); ?>
</p>
<p>
<?php echo $this->form_category->getElement('id_parent')->renderLabel(); ?>
<br>
<?php echo $this->form_category->getElement('id_parent')->renderViewHelper(); ?>
<?php echo (NULL != ($errors = $this->form_category->getElement('id_parent')->getMessages()) ? $this->formErrors($errors) : ''); ?>
</p>
<p>
<?php echo $this->form_category->getElement('description')->renderLabel(); ?>
<br>
<?php echo $this->form_category->getElement('description')->renderViewHelper(); ?>
<?php echo (NULL != ($errors = $this->form_category->getElement('description')->getMessages()) ? $this->formErrors($errors) : ''); ?>
</p>
<p>
<?php echo $this->form_category->getElement('order_by')->renderLabel(); ?>
<br>
<?php echo $this->form_category->getElement('order_by')->renderViewHelper(); ?>
<?php echo (NULL != ($errors = $this->form_category->getElement('order_by')->getMessages()) ? $this->formErrors($errors) : ''); ?>
</p>
<p>
<?php echo $this->form_category->getElement('submit_category')->renderViewHelper(); ?>
</p>
</form>
</div>
<script type="text/javascript">
$(document).ready(
function()
{
$('#name').change( slugify ).keyup( slugify );
}
);
function slugify()
{
text = $(this).val().toLowerCase();
text = text.replace(/[^-a-zA-Z0-9,&\s]+/ig, '');
text = text.replace(/-/gi, "_");
text = text.replace(/\s/gi, "-");
$('#slug').val(text);
}
</script>
Sample Category Form
Zend_Form极大地简化了crud控制器。更重要的是,如果您打算在ZF上构建应用程序,那么使用它们是公认的做法。