




版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、Magento 新聞模塊開(kāi)發(fā)教程 (一) 創(chuàng)建骨架目錄 今天突然想自己寫(xiě)一個(gè)新聞模塊,于是去參考Packt的開(kāi)發(fā)書(shū)籍,和本教程大同小異,分享給大家,宗旨還是大家能共同進(jìn)步!本教程分為9章,那么我們現(xiàn)在開(kāi)始!Magento 模塊應(yīng)當(dāng)遵循 Magento的MVC結(jié)構(gòu),包括block,controllers,etc,helper,model,sql,視圖層放在design的對(duì)應(yīng)目錄下,分為layout和template。當(dāng)然在寫(xiě)模塊之前,你必須創(chuàng)建模塊的xml啟動(dòng)文件,這個(gè)我會(huì)在后續(xù)的章節(jié)中提到。不管你用的是eclipse,netbeans還是zend studio,都應(yīng)該有如下的骨架目錄,我們把他
2、放在app/code/local下面:這樣我們便完成了對(duì)骨架目錄的創(chuàng)建,下一章我們會(huì)創(chuàng)建一個(gè)xml的模塊啟動(dòng)文件來(lái)激活這個(gè)模塊。Magento 新聞模塊開(kāi)發(fā)教程 (二) 激活你的模塊 現(xiàn)在我們應(yīng)該告訴Magento來(lái)激活這個(gè)模塊。1. 在app / etc / modules中的應(yīng)用程序創(chuàng)建一個(gè)新的XML文件,命名為INTERFACE_MODULE目錄中。就我而言,這是Danfore_News.xml2. 寫(xiě)入以下代碼,這樣,我們的模塊便激活了。 <?xml version="1.0"?> <config> <modules> <D
3、anfore_News> <active>true</active> <codePool>local</codePool> </Danfore_News> </modules> </config> Magento的配置很大程度上取決于xml文件。對(duì)于新建立的模塊,我們必須告訴magento新的包,這里是Danfore,并且告訴magento新的代碼庫(kù)放在local下面,active節(jié)點(diǎn)設(shè)置為true表示激活啟用。Magento 新聞模塊開(kāi)發(fā)教程 (三) 創(chuàng)建前后端控制器 之前創(chuàng)建了骨架目錄并且激活了新模塊
4、,現(xiàn)在要為模塊創(chuàng)建前臺(tái)和后臺(tái)的控制器。1. 首先在app/code/local/Danfore/News/controllers/ 中創(chuàng)建一個(gè)名為IndexController.php的前端控制器:class Danfore_News_IndexController extends Mage_Core_Controller_Front_Action public function indexAction() $resource = Mage:getSingleton('core/resource'); $read = $resource->getConnection(
5、39;core_read'); $newsTable = $resource->getTableName('news'); $select = $read->select()->from($newsTable, array('news_id', 'title', 'filename','content', 'status')->where('status', 1)->order('created_time DESC'); $new
6、s = $read->fetchAll($select); Mage:register('list', $news); $this->loadLayout(); $this->renderLayout(); public function viewAction() $news_id = $this->getRequest()->getParam('id'); if ($news_id != null && $news_id != '') $news = Mage:getModel('news/
7、news')->load($news_id)->getData(); else $news = null; /* * If no param we load a the last created item */ if ($news = null) $resource = Mage:getSingleton('core/resource'); $read = $resource->getConnection('core_read'); $newsTable = $resource->getTableName('news
8、9;); $select = $read->select()->from($newsTable, array('news_id', 'filename', 'title','content', 'status', 'update_time')->where('status', 1)->order('created_time DESC'); $news = $read->fetchRow($select); Mage:register(
9、39;news', $news); $this->loadLayout(); $this->renderLayout(); 這個(gè)controller是前端控制器,主要負(fù)責(zé)加載和顯示文章列表和內(nèi)容,index 控制器負(fù)責(zé)現(xiàn)實(shí)文章列表,當(dāng)用戶(hù)點(diǎn)擊指定文章時(shí),跳轉(zhuǎn)到view動(dòng)作,顯示指定文章的詳細(xì)信息,magento中訪(fǎng)問(wèn)數(shù)據(jù)庫(kù)需要?jiǎng)?chuàng)建一個(gè)core_resource對(duì)象,這個(gè)對(duì)象可以用來(lái)執(zhí)行sql語(yǔ)句。接下來(lái)創(chuàng)建一個(gè)后臺(tái)控制器NewsController.php,放在app/code/local/Danfore/News/controllers/Adminhtml/目錄下:cla
10、ss Danfore_News_Adminhtml_NewsController extends Mage_Adminhtml_Controller_action protected function _initAction() $this->loadLayout()->_setActiveMenu('news/items')->_addBreadcrumb(Mage:helper('adminhtml')->_('Items Manager'), Mage:helper('adminhtml')->
11、_('Item Manager'); return $this; public function indexAction() $this->_initAction(); $this->_addContent($this->getLayout()->createBlock('news/adminhtml_news'); $this->renderLayout(); public function editAction() $id = $this->getRequest()->getParam('id');
12、$model = Mage:getModel('news/news')->load($id); if ($model->getId() | $id = 0) $data = Mage:getSingleton('adminhtml/session')->getFormData(true); if (!empty($data) $model->setData($data); Mage:register('news_data', $model); $this->loadLayout(); $this->_setAc
13、tiveMenu('news/items'); $this->_addBreadcrumb(Mage:helper('adminhtml')->_('Item Manager'), Mage:helper('adminhtml')->_('News Item Manager'); $this->_addBreadcrumb(Mage:helper('adminhtml')->_('Item News'), Mage:helper('adminht
14、ml')->_('Item News'); $this->getLayout()->getBlock('head')->setCanLoadExtJs(true); $this->_addContent($this->getLayout()->createBlock('news/adminhtml_news_edit')->_addLeft($this->getLayout()->createBlock('news/adminhtml_news_edit_tabs'
15、;); $this->renderLayout(); else Mage:getSingleton('adminhtml/session')->addError(Mage:helper('news')->_('Item does not exist'); $this->_redirect('*/*/'); public function newAction() $this->_forward('edit'); public function saveAction() if ($data
16、 = $this->getRequest()->getPost() if (isset($_FILES'filename''name') && $_FILES'filename''name' != '') try /* Starting upload */ $uploader = new Varien_File_Uploader('filename'); / Any extention would work $uploader->setAllowedExtensio
17、ns(array('jpg', 'jpeg','gif', 'png'); $uploader->setAllowRenameFiles(false); $uploader->setFilesDispersion(false); / We set media as the upload dir $path = Mage:getBaseDir('media') . DS . 'news' . DS; $uploader->save($path, $_FILES'filenam
18、e''name'); catch (Exception $e) /this way the name is saved in DB $data'filename' = $_FILES'filename''name' $model = Mage:getModel('news/news'); $model->setData($data)->setId($this->getRequest()->getParam('id'); try if ($model->getCr
19、eatedTime = NULL | $model->getUpdateTime() = NULL) $model->setCreatedTime(now()->setUpdateTime(now(); else $model->setUpdateTime(now(); $model->save(); Mage:getSingleton('adminhtml/session')->addSuccess(Mage:helper('news')->_('Item was successfully saved'
20、); Mage:getSingleton('adminhtml/session')->setFormData(false); if ($this->getRequest()->getParam('back') $this->_redirect('*/*/edit', array('id' => $model->getId(); return; $this->_redirect('*/*/'); return; catch (Exception $e) Mage:getSin
21、gleton('adminhtml/session')->addError($e->getMessage(); Mage:getSingleton('adminhtml/session')->setFormData($data); $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id'); return; Mage:getSingleton('adminhtml/se
22、ssion')->addError(Mage:helper('news')->_('Unable to find item to save'); $this->_redirect('*/*/'); public function deleteAction() if ($this->getRequest()->getParam('id') > 0) try $model = Mage:getModel('news/news'); $model->setId($this
23、->getRequest()->getParam('id')->delete(); Mage:getSingleton('adminhtml/session')->addSuccess(Mage:helper('adminhtml')->_('Item was successfully deleted'); $this->_redirect('*/*/'); catch (Exception $e) Mage:getSingleton('adminhtml/session
24、')->addError($e->getMessage(); $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id'); $this->_redirect('*/*/'); public function massDeleteAction() $newsIds = $this->getRequest()->getParam('news'); if (!is
25、_array($newsIds) Mage:getSingleton('adminhtml/session')->addError(Mage:helper('adminhtml')->_('Please select item(s)'); else try foreach ($newsIds as $newsId) $news = Mage:getModel('news/news')->load($newsId); $news->delete(); Mage:getSingleton('adminh
26、tml/session')->addSuccess(Mage:helper('adminhtml')->_('Total of %d record(s) were successfully deleted',count($newsIds); catch (Exception $e) Mage:getSingleton('adminhtml/session')->addError($e->getMessage(); $this->_redirect('*/*/index'); public fu
27、nction massStatusAction() $newsIds = $this->getRequest()->getParam('news'); if (!is_array($newsIds) Mage:getSingleton('adminhtml/session')->addError($this->_('Please select item(s)'); else try foreach ($newsIds as $newsId) $news = Mage:getSingleton('news/news&
28、#39;)->load($newsId)->setStatus($this->getRequest()->getParam('status')->setIsMassupdate(true)->save(); $this->_getSession()->addSuccess($this->_('Total of %d record(s) were successfully updated', count($newsIds); catch (Exception $e) $this->_getSession(
29、)->addError($e->getMessage(); $this->_redirect('*/*/index'); public function exportCsvAction() $fileName = 'news.csv' $content = $this->getLayout()->createBlock('news/adminhtml')->getCsv(); $this->_sendUploadResponse($fileName, $content); public function
30、exportXmlAction() $fileName = 'news.xml' $content = $this->getLayout()->createBlock('news/adminhtml_news_grid')->getXml(); $this->_sendUploadResponse($fileName, $content); protected function _sendUploadResponse($fileName, $content,$contentType='application/octet-strea
31、m') $response = $this->getResponse(); $response->setHeader('HTTP/1.1 200 OK', ''); $response->setHeader('Pragma', 'public', true); $response->setHeader('Cache-Control', 'must-revalidate, postcheck=0, pre-check=0', true); $response->s
32、etHeader('Content-Disposition', 'attachment;filename=' . $fileName); $response->setHeader('Last-Modified', date('r'); $response->setHeader('Accept-Ranges', 'bytes'); $response->setHeader('Content-Length', strlen($content); $response-&g
33、t;setHeader('Content-type', $contentType); $response->setBody($content); $response->sendResponse(); die; 這個(gè)控制器主要負(fù)責(zé)后臺(tái)的一些動(dòng)作,比如增刪查改。在_initAction()里面,我們做了三件事:加載布局,在后臺(tái)設(shè)置活動(dòng)菜單,還有添加面包屑的文本Index action則創(chuàng)建一個(gè)news/adminhtml_news block和加載對(duì)應(yīng)布局。其他action則不一一熬述。調(diào)用edit操作時(shí),用戶(hù)點(diǎn)擊編輯鏈接在新聞列表下新聞項(xiàng)目管理一節(jié)。相關(guān)新聞的編號(hào)是從
34、ID參數(shù)和傳遞的消息模型中抓取,以獲取具體的新聞。如果給定的新聞I產(chǎn)生的結(jié)果,它傳遞的新聞形式的數(shù)據(jù)來(lái)填充與形式給定的數(shù)據(jù)。新聞編輯表單提交后,數(shù)據(jù)的saveaction傳遞與更新的消息ID。還有一些其他的任務(wù),已實(shí)現(xiàn)這個(gè)方法,如裝載布局,建立活動(dòng)的菜單,加入面包屑的文本等等。newAction()用于跳轉(zhuǎn)到edit動(dòng)作saveActon()則用于與模型層交互,保存輸入的信息deleteAction()用于刪除指定的文章massDeleteAction()則是批量刪除massStatusAction()為批量狀態(tài)修改exportCsvAction()用于到處cvs文件exportXmlActi
35、on()則用于導(dǎo)入xml文件_sendUploadResponse則是當(dāng)導(dǎo)出xml文件時(shí)設(shè)置http報(bào)頭Magento的控制器其實(shí)和zend framework的控制器工作原理一樣,控制器必須在MVC模式下工作,它操縱模型,決定哪些視圖來(lái)顯示用戶(hù)的要求和其他因素的基礎(chǔ)上傳遞數(shù)據(jù),查看每個(gè)用戶(hù)響應(yīng),或完全關(guān)閉控制并跳轉(zhuǎn)到另一個(gè)控制器。他總是保持一個(gè)控制器盡量瘦小,業(yè)務(wù)邏輯則盡量交給model來(lái)處理,skinny controller and fat model !Magento 新聞模塊開(kāi)發(fā)教程 (四) 創(chuàng)建模塊的xml配置文件 上一章我們好不容易創(chuàng)建好控制器,這里我們繼續(xù)為magento創(chuàng)建模
36、塊配置文件,在app/code/local/Danfore/News/etc下,我這里是app/code/local/Danfore/News/etc下創(chuàng)建一個(gè)config.xml文件: <?xml version="1.0"?> <config> <modules> <Danfore_News> <version>0.1.0</version> </Danfore_News> </modules> <frontend> <routers> <new
37、s> <use>standard</use> <args> <module>Danfore_News</module> <frontName>news</frontName> </args> </news> </routers> <layout> <updates> <news> <file>news.xml</file> </news> </updates> </layout&g
38、t; </frontend> <admin> <routers> <news> <use>admin</use> <args> <module>Danfore_News</module> <frontName>news</frontName> </args> </news> </routers> </admin> <adminhtml> <menu> <news module="
39、;news"> <title>News</title> <sort_order>71</sort_order> <children> <items module="news"> <title>Manage Items</title> <sort_order>0</sort_order> <action>news/adminhtml_news</action> </items> </children&
40、gt; </news> </menu> <acl> <resources> <all> <title>Allow Everything</title> </all> <admin> <children> <Danfore_News> <title>News Module</title> <sort_order>10</sort_order> </Danfore_News> </children>
41、; </admin> </resources> </acl> <layout> <updates> <news> <file>news.xml</file> </news> </updates> </layout> </adminhtml> <global> <models> <news> <class>Danfore_News_Model</class> <resourceModel&
42、gt;news_mysql4</resourceModel> </news> <news_mysql4> <class>Danfore_News_Model_Mysql4</class> <entities> <news> <table>news</table> </news> </entities> </news_mysql4> </models> <resources> <news_setup> <setu
43、p> <module>Danfore_News</module> </setup> <connection> <use>core_setup</use> </connection> </news_setup> <news_write> <connection> <use>core_write</use> </connection> </news_write> <news_read> <connection
44、> <use>core_read</use> </connection> </news_read> </resources> <blocks> <news> <class>Danfore_News_Block</class> </news> </blocks> <helpers> <news> <class>Danfore_News_Helper</class> </news> </helpe
45、rs> </global> </config> 這個(gè)文件包含了我們的新聞模塊的不同組成部分,既為前端和后臺(tái)信息。如果我們觀(guān)察該配置文件的結(jié)構(gòu),會(huì)發(fā)現(xiàn)有五個(gè)組件,1. (module)模塊:它包含版本信息2. (frontend)前端:這包含路由器和布局信息3. (admin)管理員:這是路由器的設(shè)置和管理部分的參數(shù)4. (adminhtml)后臺(tái)界面:這是菜單,ACL和布局管理5。(global)全局:它包含配置的model, resources, blocks, and helpers等具體可以參見(jiàn)官方config.xml的文檔,Magento 新聞模塊開(kāi)發(fā)教程
46、 (五) 創(chuàng)建輔助類(lèi) 在MVC應(yīng)用程序中,模型處理業(yè)務(wù)邏輯和數(shù)據(jù)返回到控制器,控制器最終傳遞數(shù)據(jù)給視圖。如果我們需要一個(gè)相當(dāng)復(fù)雜的需求邏輯需要重復(fù)或我們不希望它放置在一個(gè)視圖文件,Helper組件將有助于處理復(fù)雜的情況。在大多數(shù)情況下,一個(gè)輔助類(lèi)幫助組織一個(gè)像樣的數(shù)據(jù)邏輯,可以用他來(lái)反復(fù)處理。在我們這章中,我們將為我們的新聞模塊創(chuàng)建一個(gè)空Helper文件,也是Magento輔助類(lèi)的經(jīng)常需要處理的情況:1 訪(fǎng)問(wèn)模型2 執(zhí)行復(fù)雜的或重復(fù)的顯示邏輯3 操作和格式化模型數(shù)據(jù)4 傳遞視圖腳本之間的數(shù)據(jù)那么這里,我們會(huì)在appcodelocalDanforeNewsHelper下面創(chuàng)建一個(gè)輔助類(lèi)Data.
47、php <?php class Danfore_News_Helper_Data extends Mage_Core_Helper_Abstract 這個(gè)文件是我們的新聞模塊的一部分。如果我們?cè)敢?,我們可以添加一些方法。這里是空的,因?yàn)槲覀兊男侣勀K沒(méi)有任何復(fù)雜的邏輯。但是你應(yīng)該知道什么是Magento的輔助類(lèi),并且在什么時(shí)候它能派上用場(chǎng)!Magento 新聞模塊開(kāi)發(fā)教程 (六) 創(chuàng)建模型 創(chuàng)建好輔助類(lèi)后,接下來(lái)為我們的新聞模塊創(chuàng)建相關(guān)的模型。在MVC結(jié)構(gòu)中,Model (模型)用于處理業(yè)務(wù)邏輯,在Magento中,模型用于更新數(shù)據(jù)庫(kù)記錄,和web 服務(wù)器交互等等。模型是MVC結(jié)構(gòu)中非常
48、重要的部分,Magento的模型擴(kuò)展自Zend_Db_Table_Abstract 類(lèi),這個(gè)類(lèi)可以連接Varien對(duì)象,然后訪(fǎng)問(wèn)數(shù)據(jù)庫(kù)。這里我們會(huì)為新聞模塊創(chuàng)建對(duì)應(yīng)的模型。在appcodelocalDanforeNewsModel下面,創(chuàng)建一個(gè)名為News.php的文件: <?php class Danfore_News_Model_News extends Mage_Core_Model_Abstract public function _construct() parent:_construct(); $this->_init('news/news'); ?&g
49、t; 在appcodelocalDanforeNewsModelMysql4下創(chuàng)建多一個(gè)News.php文件: <?php class Danfore_News_Model_Mysql4_News extends Mage_Core_Model_Mysql4_Abstract public function _construct() / Note that the news_id refers to the key field in your database table. $this->_init('news/news', 'news_id'); ?> 然后再appcodelocalDanforeNewsModelMy
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 互動(dòng)式教學(xué)工具的營(yíng)銷(xiāo)策略與教育內(nèi)容創(chuàng)新
- 抖音商戶(hù)直播設(shè)備故障應(yīng)急切換制度
- 全球視野下2025年跨文化交流能力在國(guó)際化教育中的核心地位報(bào)告
- 公交優(yōu)先發(fā)展戰(zhàn)略2025:城市交通擁堵治理中的公共交通與公共交通市場(chǎng)拓展研究報(bào)告
- CLK2-3-IN-1-生命科學(xué)試劑-MCE
- 陜西青年職業(yè)學(xué)院《口譯理論與實(shí)踐》2023-2024學(xué)年第一學(xué)期期末試卷
- 新疆農(nóng)業(yè)職業(yè)技術(shù)學(xué)院《基礎(chǔ)泰語(yǔ)》2023-2024學(xué)年第一學(xué)期期末試卷
- 宣城職業(yè)技術(shù)學(xué)院《煙草文化》2023-2024學(xué)年第一學(xué)期期末試卷
- 公共衛(wèi)生應(yīng)急物資儲(chǔ)備體系建設(shè)2025年實(shí)施方案與信息化建設(shè)報(bào)告
- 吉林省長(zhǎng)春市新區(qū)2024-2025學(xué)年九年級(jí)化學(xué)第一學(xué)期期末質(zhì)量檢測(cè)模擬試題含解析
- 天津小學(xué)生詩(shī)詞大賽備考試題庫(kù)500題(五六年級(jí))
- GB/T 15231-2023玻璃纖維增強(qiáng)水泥性能試驗(yàn)方法
- 外出提攜公章申請(qǐng)表
- 心內(nèi)科值班專(zhuān)業(yè)知識(shí)講座
- 2022年廣州市黃埔區(qū)輔警考試試卷真題
- 國(guó)家開(kāi)放大學(xué)電大本科《婦產(chǎn)科學(xué)與兒科護(hù)理學(xué)(本)》期末題庫(kù)及答案
- 2023版押品考試題庫(kù)必考點(diǎn)含答案
- 井下變電所停送電操作規(guī)程
- 【本田轎車(chē)燈光系統(tǒng)常見(jiàn)故障分析及排除8200字(論文)】
- 我的妹妹高中 我的妹妹初中800字(三篇)
- 昆明天大礦業(yè)有限公司尋甸縣金源磷礦老廠(chǎng)箐-小凹子礦段(擬設(shè))采礦權(quán)出讓收益評(píng)估報(bào)告
評(píng)論
0/150
提交評(píng)論