Magento 添加后台管理

后台菜单显示点击后404,如果adminhtml.xml配置正确,那是config.xml的问题

Magento Grid关联了多表后,表与表之间有相同字段出现。在后台点查询时出现报错解决用filter_index

Java代码   收藏代码
  1. $this->addColumn('name', array(  
  2.     'header' => '返利商家',  
  3.     'align' => 'right',  
  4.     'width' => '50px',  
  5.     'index' => 'vname',  
  6.     'filter_index' =>'v.name',  
  7. ));  
  8. $this->addColumn('type', array(  
  9.     'header' => '返利规则名',  
  10.     'align' => 'right',  
  11.     'width' => '50px',  
  12.     'index' => 'name',  
  13.     'filter_index' =>'main_table.name',  
  14. ));  

1.在后台管理界面加入菜单

1.1添加新菜单

Java代码   收藏代码
  1. "1.0"?>  
  2.   
  3.       
  4.           
  5.         "title">  
  6.             推荐位管理  
  7.             110  
  8.               
  9.                 "title">  
  10.                     商品推荐  
  11.                     adminhtml/recommend_product  
  12.                   
  13.                 "title">  
  14.                     积分商户推荐  
  15.                     adminhtml/recommend_jifen  
  16.                   
  17.                 "title">  
  18.                     图片推荐  
  19.                     adminhtml/recommend_image  
  20.                   
  21.               
  22.           
  23.       
  24.       
  25.           
  26.               
  27.                   
  28.                     "title">  
  29.                         推荐位管理  
  30.                           
  31.                             "title">  
  32.                                 商品推荐  
  33.                                 0  
  34.                               
  35.                             "title">  
  36.                                 商品推荐  
  37.                                 0  
  38.                               
  39.                             "title">  
  40.                                 图片推荐  
  41.                                 0  
  42.                               
  43.                           
  44.                       
  45.                   
  46.               
  47.           
  48.       
  49.   

Magento 添加后台管理_第1张图片
 

1.2在已有菜单下添加:在模块的Glamour/CustomerMessage/etc目录下增加adminhtml.xml配置文件,用于加入自定义的菜单项。文件内容如下:

Java代码   收藏代码
  1. "1.0"?>  
  2.   
  3.       
  4.           
  5.           
  6.           
  7.           
  8.               
  9.                   
  10.                 "title">  
  11.                     Customer Message  
  12.                   
  13.                     adminhtml/message  
  14.                     120  
  15.                   
  16.               
  17.            
  18.       
  19.      
  20.       
  21.           
  22.               
  23.                   
  24.                       
  25.                           
  26.                             "title">  
  27.                                 Customer Message  
  28.                               
  29.                           
  30.                       
  31.                   
  32.               
  33.           
  34.       
  35.   

可以在后台,系统-》权限-》角色-》角色资源-》自定义 查看

2开发后台Controller

在模块的controllers目录下创建Adminhtml目录,新建一个Controller类。

Java代码   收藏代码
  1. class Glamour_CustomerMessage_Adminhtml_MessageController extends Mage_Adminhtml_Controller_Action {  
  2.   
  3.     public function indexAction() {  
  4.         $this->loadLayout()  
  5.             ->_setActiveMenu('customer')  
  6.             ->_addBreadcrumb('Customer Message''Customer Message')  
  7.         ->renderLayout();  
  8.     }  
  9.   
  10.     protected function _initData() {  
  11.         $id = $this->getRequest()->getParam('id'0);  
  12.         $model = Mage::getModel('news/news');  
  13.         if ($id > 0) {  
  14.             $model = $model->load($id);  
  15.         }  
  16.         Mage::register('form_data', $model);  
  17.   
  18.         return $formdata;  
  19.     }  
  20.   
  21.     public function editAction() { //页面回显  
  22.     if (!$formdata = $this->_initData()) {  
  23.             return;  
  24.         }  
  25.         try{  
  26.             $this->loadLayout();  
  27.             $this->_setActiveMenu('customer/message');  
  28.             $this->_addContent($this->getLayout()->createBlock('news/adminhtml_news_edit'))  
  29.                 ->_addLeft($this->getLayout()->createBlock('news/adminhtml_news_edit_tabs'));  
  30.             $this->renderLayout();  
  31.         } catch(Exception $e) {  
  32.             Mage::getSingleton('adminhtml/session')->addError('记录不存在');  
  33.             $this->_redirect('*/*/');  
  34.         }  
  35.     }  
  36.   
  37.     public function newAction() {  
  38.         $this->_forward('edit');  
  39.     }  
  40.   
  41.     public function deleteAction() {  
  42.         $id = $this->getRequest()->getParam('id');  
  43.         if ($id > 0) {  
  44.             try {  
  45.                 $this->doDelete($id);  
  46.                 Mage::getSingleton('adminhtml/session')->addSuccess('删除成功');  
  47.                 $this->_redirect('*/*/');  
  48.             } catch (Exception $e) {  
  49.                 Mage::getSingleton('adminhtml/session')->addError('删除失败');  
  50.                 $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));  
  51.             }  
  52.         }  
  53.         $this->_redirect('*/*/');  
  54.     }  
  55.   
  56.     public function saveAction() {  
  57.         if ($data = $this->getRequest()->getPost()) {  
  58.             try {  
  59.                 $model = Mage::getModel('news/news');  
  60.                 $model->setData($data);  
  61.                 $model->save();  
  62.                 Mage::getSingleton('adminhtml/session')->addSuccess('保存成功');  
  63.                 // check if 'Save and Continue'  
  64.                 if ($this->getRequest()->getParam('back')=='edit') {  
  65.                     $this->_redirect('*/*/edit', array('id' => $model->getId()));  
  66.                     return;  
  67.                 }  
  68.                 $this->_redirect('*/*/');  
  69.             } catch (Exception $e) {  
  70.                 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());  
  71.                 $this->_redirect('*/*/edit', array('id' => $model->getId()));  
  72.                 return;  
  73.             }  
  74.         }  
  75.         $this->_redirect('*/*/');  
  76.     }  
  77.   
  78.     public function massDeleteAction() {  
  79.         $ids = $this->getRequest()->getParam('news');  
  80.         if (!empty($ids)) {  
  81.             try {  
  82.                 $this->doDelete($ids);  
  83.                 Mage::getSingleton('adminhtml/session')->addSuccess(sprintf('删除 %d条成功', count($ids)));  
  84.             } catch (Exception $e) {  
  85.                 Mage::getSingleton('adminhtml/session')->addError('删除失败');  
  86.             }  
  87.         }  
  88.         $this->_redirect('*/*/');  
  89.     }  
  90.   
  91.     private function doDelete($id) {  
  92.         if (is_array($id)) {  
  93.             foreach ($id as $id) {  
  94.                 $this->doDelete($id);  
  95.             }  
  96.         } else {  
  97.             Mage::getModel('news/news')->load($id)->delete();  
  98.         }  
  99.     }  
  100.   
  101.     public function exportCsvAction() {  
  102.         $fileName = 'fanli.csv';  
  103.         $grid = $this->getLayout()->createBlock('rebates/adminhtml_order_grid');  
  104.         $this->_prepareDownloadResponse($fileName, strip_tags($grid->getCsv()));   //string 方式  
  105. //      $this->_prepareDownloadResponse($fileName, $grid->getCsvFile($fileName)); //读export/xml方式导出,先写文件再读文件,性能不好  
  106.     }  
  107. }  

后台的Controller类继承自Mage_Adminhtml_Controller_Action,在etc/config.xml加入:

Java代码   收藏代码
  1.   
  2.   
  3.        
  4.           
  5.               
  6.                   
  7.                     "Mage_Adminhtml">Glamour_CustomerMessage_Adminhtml  
  8.                   
  9.               
  10.           
  11.       
  12.   

这样就可以使用和Magento自带的后台模块类似的url .../index.php/admin/message/来访问自定义模块的后台controller,也可以实现rewrite后台controller。

 

3开发后台Block

在app\code\local\Voodoo\News\Block\Adminhtml下,建立News.php文件:列表显示

Java代码   收藏代码
  1. class Voodoo_News_Block_Adminhtml_News extends Mage_Adminhtml_Block_Widget_Grid_Container  
  2. {  
  3.     public function __construct()  
  4.     {  
  5.         $this->_controller = 'adminhtml_news'//block路径  
  6.         $this->_blockGroup = 'news';//module name  
  7.         $this->_headerText = '管理';  
  8.         $this->_addButtonLabel = '添加';  
  9.         parent::__construct();  
  10. //$this->_removeButton('back');//reset save add  
  11.     }  
  12. }   

 同样目录下继续建立Grid.php文件:

Java代码   收藏代码
  1. class Voodoo_News_Block_Adminhtml_News_Grid extends Mage_Adminhtml_Block_Widget_Grid {  
  2.   
  3.     public function __construct() {  
  4.         parent::__construct();  
  5.         $this->setId('newsGrid');  
  6.         $this->setDefaultSort('news_id');  
  7.         $this->setDefaultDir('ASC');  
  8.         $this->setSaveParametersInSession(true);  
  9.     }  
  10.   
  11.     protected function _prepareCollection() {  
  12.         $collection = Mage::getModel('news/news')->getCollection();  
  13.         $this->setCollection($collection);  
  14.         return parent::_prepareCollection();  
  15.     }  
  16.   
  17.     protected function _prepareColumns() {  
  18.         $this->addColumn('news_id', array(  
  19.             'header' => 'ID',  
  20.             'align' => 'right',  
  21.             'width' => '50px',  
  22.             'index' => 'news_id',  
  23.         ));  
  24.         $this->addColumn('action', array(  
  25.             'header' => '操作',  
  26.             'width' => '100',  
  27.             'type' => 'action',  
  28.             'getter' => 'getId',  
  29.             'actions' => array(  
  30.                 array(  
  31.                     'caption' => '修改',  
  32.                     'url' => array('base' => '*/*/edit'),  
  33.                     'field' => 'id'  
  34.                 )  
  35.             ),  
  36.             'filter' => false,  
  37.             'sortable' => false,  
  38.             'index' => 'stores',  
  39.             'is_system' => true,  
  40.         ));  
  41.         $this->addExportType('*/*/exportCsv''CSV');  
  42.         return parent::_prepareColumns();  
  43.     }  
  44.   
  45.     protected function _prepareMassaction() {  
  46.         $this->setMassactionIdField('news_id');  //checkbox value  需要_prepareMassactionColumn  
  47.         $this->getMassactionBlock()->setFormFieldName('news'); //checkbox name  
  48.         $this->getMassactionBlock()->addItem('delete', array(  
  49.             'label' => '删除',  
  50.             'url' => $this->getUrl('*/*/massDelete'),  
  51.             'confirm' => '确认删除?'  
  52.         ));  
  53.         return $this;  
  54.     }  
  55.   
  56.     protected function _prepareMassactionColumn() {  
  57.         $columnId = 'massaction';  
  58.         $massactionColumn = $this->getLayout()->createBlock('adminhtml/widget_grid_column')  
  59.             ->setData(array(  
  60.             'index'     => $this->getMassactionIdField(),  
  61.             'type'      => 'massaction',  
  62.             'name'      => $this->getMassactionBlock()->getFormFieldName(),  
  63.             'align'     => 'center',  
  64.             'disabled_values' => array(13),  // checkbox disabled values  
  65.             'is_system' => true//系统自带导出csv/excel中不显示  
  66.             'use_index' =>true  // 关联setMassactionIdField  
  67.         ));  
  68.   
  69.         if ($this->getNoFilterMassactionColumn()) {  
  70.             $massactionColumn->setData('filter'false);  
  71.         }  
  72.   
  73.         $massactionColumn->setSelected($this->getMassactionBlock()->getSelected())  
  74.             ->setGrid($this)  
  75.             ->setId($columnId);  
  76.   
  77.         $oldColumns = $this->_columns;  
  78.         $this->_columns = array();  
  79.         $this->_columns[$columnId] = $massactionColumn;  
  80.         $this->_columns = array_merge($this->_columns, $oldColumns);  
  81.         return $this;  
  82.     }  
  83.   
  84.     public function getRowUrl($row) //点击tr进入edit,去掉后显示#  
  85.     {  
  86.         return $this->getUrl('*/*/edit', array('id' => $row->getId()));  
  87.     }  
  88. }  

 app\code\local\Voodoo\News\Block\Adminhtml\News中建立Edit.php文件:

Java代码   收藏代码
  1. class Voodoo_News_Block_Adminhtml_News_Edit extends Mage_Adminhtml_Block_Widget_Form_Container {  
  2.     //protected $_mode = 'edit';//form.php目录名  
  3.   
  4.     public function __construct() {  
  5.         parent::__construct();  
  6.         $this->_objectId = 'id';  
  7.         $this->_blockGroup = 'news';  
  8.         $this->_controller = 'adminhtml_news';  
  9.   
  10.         $this->_updateButton('save''label''保存');  
  11.         $this->_updateButton('delete''label''删除');  
  12.         $this->_addButton('saveandcontinue', array(  
  13.             'label'     => Mage::helper('adminhtml')->__('Save and Continue Edit'),  
  14.             'onclick'   => 'saveAndContinueEdit()',  
  15.             'class'     => 'save',  
  16.         ), -100);  
  17.   
  18.         $this->_formScripts[] =<<
  19.             function toggleEditor() {  
  20.                 if (tinyMCE.getInstanceById('block_content') == null) {  
  21.                     tinyMCE.execCommand('mceAddControl'false'block_content');  
  22.                 } else {  
  23.                     tinyMCE.execCommand('mceRemoveControl'false'block_content');  
  24.                 }  
  25.             }  
  26.   
  27.             function saveAndContinueEdit(){  
  28.                 editForm.submit(\$('edit_form').action+'back/edit/');  
  29.             }  
  30. JS;  
  31.     }  
  32.   
  33.     public function getHeaderText() {  
  34.         if (Mage::registry('form_data') && Mage::registry('form_data')->getId()) {  
  35.             return ("Edit Item '%s'", $this->htmlEscape(Mage::registry('news_data')->getTitle());  
  36.         } else {  
  37.             return 'Add Item';  
  38.         }  
  39.     }  
  40.   
  41.     public function _prepareLayout() {  //add js css 需要添加此方法,其他可以删除  
  42.         $this->getLayout()->getBlock('head')  
  43.             ->addJs('extjs/ext-tree.js')  
  44.             ->addJs('extjs/ext-tree-checkbox.js')  
  45.             ->addItem('js_css''extjs/resources/css/ext-all.css');  
  46.   
  47.         return parent::_prepareLayout();  
  48.     }  
  49. }  

app\code\local\Voodoo\News\Block\Adminhtml\News\Edit下创建Form.php和Tabs.php:

Java代码   收藏代码
  1. class Voodoo_News_Block_Adminhtml_News_Edit_Form extends Mage_Adminhtml_Block_Widget_Form  
  2. {  
  3.     protected function _prepareForm()  
  4.     {  
  5.         $form = new Varien_Data_Form(array(  
  6.             'id'        => 'edit_form',  
  7.             'action'    => $this->getUrl('*/*/save', array('id' =>$this->getRequest()->getParam('id'))),  
  8.             'method'    => 'post',  
  9.             'enctype'   => 'multipart/form-data'  
  10.         ));  
  11.         $form->setUseContainer(true);  
  12.         $this->setForm($form);  
  13.         return parent::_prepareForm();  
  14.     }  
  15. }  

 下面的是tab选项卡带form,可以不要。将$fieldset的内容放到上面去

Java代码   收藏代码
  1. class Voodoo_News_Block_Adminhtml_News_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs  
  2. {  
  3.     public function __construct()  
  4.     {  
  5.         parent::__construct();  
  6.         $this->setId('news_tabs');  
  7.         $this->setDestElementId('edit_form');  
  8.         $this->setTitle('News Information');  
  9.     }  
  10.   
  11.     protected function _beforeToHtml()  
  12.     {  
  13.         $this->addTab('form_section', array(  
  14.             'label' => 'News Information',  
  15.             'title' => 'News Information',  
  16.             'content' => $this->getLayout()->createBlock('news/adminhtml_news_edit_tab_form')->toHtml(),  
  17.         ));  
  18.         return parent::_beforeToHtml();  
  19.     }  
  20. }  

 最后在app\code\local\Voodoo\News\Block\Adminhtml\News\Edit\Tab下建立Form.php

Java代码   收藏代码
  1. class Voodoo_News_Block_Adminhtml_News_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form {  
  2.   
  3.     /*public function __construct() { 
  4.         parent::__construct(); 
  5.         $this->setTemplate('news/news/tab/form.phtml'); //用setTemplate时不需要用_prepareForm 
  6.     }*/  
  7.     protected function _prepareForm() {  
  8.         $form = new Varien_Data_Form();  
  9.   
  10.         $fieldset = $form->addFieldset('news_form', array('legend' => 'Item information'));  
  11.         $fieldset->addField('title''text', array('label' => 'Title''class' => 'required-entry''required' => true'name' => 'title'));  
  12.   
  13.         $model = Mage::registry('form_data');  
  14.         $form->setValues($model->getData());  
  15.         $this->setForm($form);  
  16.         return parent::_prepareForm();  
  17.     }  
  18. }  

这样就完成了模块的创建。block是Magento的重要部分,这里我们创建了7个新闻模块功能,我们写了一些方法,并且将数据传递到布局。

这样我们便完成了新闻模块的开发,这是后台截图:
Magento 添加后台管理_第2张图片

相信留心的人已经在看Mage_Widget模块了,对的就是从这里学到的。像这种例子还可以参考Mage_Centinel。目前我只能向大家讲些框架的理论,具体里面的业务逻辑,还要靠自己多看、多理解。学习Magento绝对是个体力活,希望大家能够少走些弯路。

你可能感兴趣的:(Magento 添加后台管理)