閱讀773 返回首頁    go 阿裏雲 go 技術社區[雲棲]


magento 開發 -- 深入理解Magento第七章 – 自定義Magento係統配置

 

第七章 – 自定義Magento係統配置

Magento擁有十分強大的後台管理係統。作為一名開發人員,這套後台管理係統可以讓你的用戶簡單直接的配置Magento係統或者你創建的模塊。和Magento的其他功能一樣,你第一次使用這套管理係統的時候可能覺得很麻煩,但是一旦你上手了,你會發現它強大的功能是那麼吸引人。那麼讓我們開始吧。我們這一章的例子依然是基於Helloworld模塊。

添加係統配置文件

首先我們要為模塊添加一個係統配置文件。這個文件和“config.xml”是不搭界的
app/code/local/Zhlmmc/Helloworld/etc/system.xml
和全局配置(global config)相似,係統配置也是單獨存儲的。我們可以通過下麵這段代碼來獲取係統配置文件
//header('Content-Type: text/xml');         
header('Content-Type: text/plain');         
echo $config = Mage::getConfig()
->loadModulesConfiguration('system.xml')        
->getNode()
->asXML();          
exit;

你可以把這段代碼放到任何執行函數(Action Method)中。“loadModulesConfiguration”方法會搜索所有配置好的模塊的“etc”文件夾,尋找以傳入的參數為名字的文件,在這個例子中是“system.xml”。Magento有很多不同的配置文件,比如api.xml, wsdl.xml, wsdl2.xml, convert.xml, compilation.xml, install.xml。你可以為你創建的模塊創建這些配置文件。

添加一個標簽頁

我們首先在後台係統管理頁麵添加一個標簽頁(Tab)。標簽頁就是後台“System->Configuration”頁麵左側的導航欄。默認的標簽頁有General,Catalog,Customers,Sales,Services等等。我們來創建一個新的標簽頁叫做“Hello Config”。創建如下文件
Location: app/code/local/Zhlmmc/Helloworld/etc/system.xml
<config>
    <tabs>
        <helloconfig translate="label" module="helloworld">
            <label>Hello Config</label>
            <sort_order>99999</sort_order>
        </helloconfig>
    </tabs> 
</config>

我們來解釋一下各個節點(Tag)的意思。【譯者注:由於Tab和Tag中文翻譯都是標簽,所以這裏我把Tag翻譯成節點,以免混淆】“<helloconfig>”就是我們要添加的標簽頁的定義節點,“helloconfig”是節點的ID。你可以任意命名這個ID,但是必須全局唯一,也就是不能和別人用同樣的ID。這個ID是用來唯一標示你的標簽頁的。“module=helloworld”,意思是這個標簽頁屬於哪個模塊。“<label>”節點的內容是標簽的名字,也就是要顯示在界麵上的名字。“<sort_order>”指明了這個標簽頁顯示的位置。

打開後台“System->Configuration”,你會看到如下錯誤
Fatal error: Class 'Mage_Helloworld_Helper_Data' not found in….

Magento Helper簡介

正如許多其他的PHP MVC係統一樣,Magento也有幫助類(Helper Classes)。這些類用來提供一些不適合放在模型,視圖或者控製器中的功能。Magento的幫助類也是采用分組類名的機製。也就是說我們可以覆蓋默認的幫助類,同時我們需要在config.xml中指定幫助類的基類名。

Magento係統默認模塊有一個默認的幫助類。正如我們上麵的異常顯示,我們的Helloworld模塊並沒有指定一個默認的幫助類。下麵讓我們來添加一個。修改config.xml
File: app/code/local/Zhlmmc/Helloworld/etc/config.xml
<!– … –>
<global>
    <!– … –>
    <helpers>
        <helloworld>
            <class>Zhlmmc_Helloworld_Helper</class>
        </helloworld>
    </helpers>
    <!– … –>
</global>
<!– … –>

你現在應該對這類配置相當熟悉了。“<helloworld>”節點就是模塊的名字,“<class>”就是幫助類的基類名,命名方式如下
Packagename_Modulename_Helper
幫助類是通過全局對象Mage的靜態方法“helper”來裝載的。
Mage::helper('helloworld/foo')
根據我們的配置,上麵這行代碼將會裝載以下類
app/code/local/Zhlmmc/Helper/Foo.php
class Zhlmmc_Helloworld_Helper_Foo

我們上麵說過Magento默認每個模塊有一個幫助類“data”
Mage::helper('helloworld');
Mage::helper('helloworld/data');

上麵這兩行代碼是等價的,都會裝載以下類
app/code/local/Zhlmmc/Helper/Data.php
class Zhlmmc_Helloworld_Helper_Data

下麵我們來創建我們的幫助類
File: app/code/local/Zhlmmc/Helper/Data.php
class Zhlmmc_Helloworld_Helper_Data extends Mage_Core_Helper_Abstract
{

清空Magento緩存,重新裝載頁麵,你會發現錯誤不見了,但是我們的標簽頁還是沒有出來。如果你好奇幫助類究竟能幹什麼,建議你去看看“Mage_Core_Helper_Abstract”類。

添加新的段

好了,幫助類的介紹到此結束。下麵我們來看看為什麼我們的標簽頁不顯示出來。在Magento中,每一個標簽頁都包含很多段(section)。舉個例子,“Advanced”標簽頁默認包含“Admin, System, Advanced, Developer”四個段。如果一個標簽頁不包含任何段,那麼這個標簽頁不會被顯示出來。下麵我們在system.xml中添加“<section>”節點
Location: app/code/local/Zhlmmc/Helloworld/etc/system.xml
<config>
    <tabs>
        <helloconfig translate="label" module="helloworld">
            <label>Hello Config</label>
            <sort_order>99999</sort_order>
        </helloconfig>
    </tabs> 
    <sections>
        <helloworld_options translate="label" module="helloworld">
            <label>Hello World Config Options</label>
            <tab>helloconfig</tab>
            <frontend_type>text</frontend_type>
            <sort_order>1000</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>                    
        </helloworld_options>
    </sections>     
</config>

這裏有些節點你應該很熟悉,就不多解釋了,來講講以前沒見過的。

什麼是<helloworld_options />?

和前麵的<hellowconfig>相似,這個節點是用來唯一標示你的段,“helloworld_options”就是段的ID,可以隨意取名,隻要不重複就好。

什麼是<frontend_type />?

這個節點有點搞。“<frontend_type />”在配置文件的其他部分有用(稍後會講),放在這裏其實沒什麼作用。但是核心模塊在此處的配置文件都包含這個節點,所以我們也把它添加進去。

什麼是<show_in_default />,< show_in_website />,<show_in_store />?

這些節點的值是布爾類型的,0或者1。這些標簽是用來控製在不同的環境下,當前段是否應該顯示。

好了,我們已經配置好段了,清空緩存,再一次刷新頁麵,你應該看到“HELLO CONFIG”標簽頁顯示出來了。

訪問控製

如果你剛才點了我們創建的標簽頁下麵的“Hello World Config Options”,你大概會很失望。什麼都沒有顯示出來,連左邊的導航欄都沒有了。這是因為“Adminhtml”在權限控製列表(Access Control List, ACL)中找不到我們創建的段的權限信息。【譯者注:Adminhtml就是Magento的後台管理係統,屬於Magento的一個核心模塊】

ACL是一個很複雜的話題,但是我會介紹一些最基本的概念,以便於理解Magento的權限控製。這部分內容和上下文關係不大,如果你不感興趣,可以直接跳到本節結尾,複製一段XML到你的config.xml就行了。

在Magento中,對於有些資源的訪問時有限製的。用戶必須先經過認證才能訪問相關資源。在這裏,資源(Resource)是一個廣義的概念,它可能是指一個頁麵,也可能是一個功能。Magento的係統配置功能(System Config)就是需要認證才能訪問的資源。

任何一個資源都是通過一個URI來標識。比如說“web”配置段(屬於後台管理General標簽頁)的URI是
admin/system/config/web
我們“helloworld_options”段的URI是
admin/system/config/helloworld_options
當一個用戶訪問一個受保護的資源的時候,後台管理係統(Adminhtml)的執行控製器會執行以下步驟

  1. 為用戶正在訪問的資源生成一個URI
  2. 根據ACL係統檢查該用戶是否有權限訪問指定的資源
  3. 如果用戶擁有訪問權限,那麼進行用戶指定的操作。否則,跳轉到相應的錯誤頁麵(也可能是停止操作或者顯示空白頁麵)。

如果你去“System -> Permissions -> Roles”頁麵,點擊“Add New Role”按鈕,你會看到所有係統的資源都以樹形結構顯示在頁麵上。

添加ACL權限

剛才說ACL中沒有我們配置段的信息,那麼我們來創建一個。請注意,如果你是創建一個新的段,那麼你需要創建一個新的權限,如果你在已有的段上添加內容,你不需要創建權限。

在config.xml中,添加以下內容
File: app/code/local/Zhlmmc/Helloworld/etc/config.xml  
<config>    
    <!– … –>
    <adminhtml>
        <acl>
            <resources>
                <admin>
                    <children>
                        <system>
                            <children>
                                <config>
                                    <children>
                                        <helloworld_options>
                                            <title>Store Hello World Module Section</title>
                                        </helloworld_options>
                                    </children>
                                </config>
                            </children>
                        </system>
                    </children>
                </admin>
            </resources>
        </acl>
    </adminhtml>
    <!– … –>
</config>

讓我們來分析一下這段代碼。所有的資源定義都包含在如下代碼中
<adminhtml>
    <acl>
        <resources>
        </resource>
    </acl>
</adminhtml>

在<resources>節點下麵,每一個子節點都是URI的一部分,比如
<admin>
    <children>
        <system>
            <children>

代表URI
admin/system
最後一個節點
<helloworld_options>
    <title>Store Hello World Module Section</title>
</helloworld_options>

這裏<title>的內容將會在後台權限管理的時候顯示出來,也就是我們定義的權限的名字。

清空Magento緩存,刷新頁麵,你應該能看到我們創建的配置段了,標準的後台管理頁麵,但是主體內容是空的,隻有一個“Save Config”按鈕。你可能需要重新登錄後台管理才能看到正確的頁麵。那是因為後台管理有一些額外的緩存。【譯者注:我們添加了權限以後,管理員是默認擁有該權限的,所以我們用管理員登錄後台管理係統就能訪問我們創建的段】

請注意,不懂事出於什麼原因,Magento把<adminhtml />部分從全局配置中刪掉了。所以,我們不能用之前創建的Configviewer來查看這部分內容。我正在研究Magento把<adminhtml />存在哪裏了。

添加組

【譯者注:按照邏輯,這裏應該講的內容是添加選項。Mageto中,選項是按照組(Group)來劃分的,所以我們在添加選項之前得先添加組。】修改system.xml
Location: app/code/local/Zhlmmc/Helloworld/etc/system.xml
<config>
    <tabs>
        <helloconfig translate="label" module="helloworld">
            <label>Hello Config</label>
            <sort_order>99999</sort_order>
        </helloconfig>
    </tabs> 
    <sections>
        <helloworld_options translate="label" module="helloworld">
            <label>Hello World Config Options</label>
            <tab>helloconfig</tab>
            <frontend_type>text</frontend_type>
            <sort_order>1000</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
            <groups>
                <messages translate="label">
                    <label>Demo Of Config Fields</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>1</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                </messages>
            </groups>
        </helloworld_options>
    </sections>     
</config>

這裏也沒什麼好解釋的。刷新一下頁麵看看你就什麼都明白了。

添加配置選項

最後,我們要添加每一個單獨的配置選項。配置選項是以<fields />節點的形式添加到<messages />節點下麵的。
<!– … –>
<messages translate="label">
    <label>Demo Of Config Fields</label>
    <frontend_type>text</frontend_type>
    <sort_order>1</sort_order>
    <show_in_default>1</show_in_default>
    <show_in_website>1</show_in_website>
    <show_in_store>1</show_in_store>
    <fields>
        <hello_message>
            <label>Message</label>
            <frontend_type>text</frontend_type>
            <sort_order>1</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
        </hello_message>
    </fields>
</messages>
<!– … –>

這裏有一個節點需要說明,“<frontend_type>”,剛才說這個節點沒什麼用。但是這裏有用了,這個節點說明了這個選項的數據類型。你可以把它換成別的類型,比如“time”。這裏支持大部分默認的Varien定義的數據類型(lib/Varien/Data/Form/Element)。這個有點像是工廠(Factory)設計模式。讓我們把類型改成“select”。你會看到一個下拉框,但是沒有選項。我們來添加選項。首先我們要添加一個源模型(Source Model)
<hello_message>
    <label>Message</label>
    <frontend_type>select</frontend_type>
    <!– adding a source model –>
    <source_model>helloworld/words</source_model>                           
    <sort_order>1</sort_order>
    <show_in_default>1</show_in_default>
    <show_in_website>1</show_in_website>
    <show_in_store>1</show_in_store>                    
</hello_message>

“<source_model>”定義了源模型的URI。和我們以前創建的模型一樣,源模型也是一個模型,為“select”提供了默認的數據。我想我不說你也明白,根據這裏的URI定義,我們要創建以下文件
File: app/code/local/Zhlmmc/Helloworld/Model/Words.php
class Zhlmmc_Helloworld_Model_Words
{
    public function toOptionArray()
    {
        return array(
            array('value'=>1, 'label'=>Mage::helper('helloworld')->__('Hello')),
            array('value'=>2, 'label'=>Mage::helper('helloworld')->__('Goodbye')),
            array('value'=>3, 'label'=>Mage::helper('helloworld')->__('Yes')),
            array('value'=>4, 'label'=>Mage::helper('helloworld')->__('No')),
        );
    }
}

源模型提供了一個方法“toOptionsArray”,返回的數據時用來填充我們之前定義的配置選項的。這個方法在運行時會被“initFields”調用。“initFields”在以下類中定義
app/code/core/Mage/Adminhtml/Block/System/Config/Form.php
我們這裏調用了幫助類的翻譯函數(__)來獲取數據。雖然不是很必要,但調用翻譯函數總是一個好習慣。說不定哪天你要將模塊翻譯成日文呢。【譯者注:值得注意的是我們這裏創建的模型不需要繼承任何父類,隻需要擁有“toOptionArray”方法就可以了。我覺得這個很不科學,起碼要繼承一個接口吧】

在已有的配置段或者組中添加數據

除了新建一個標簽頁,或者配置段,你也可以利用已有的標簽頁和配置段,向裏麵添加內容。比如我們添加以下代碼到system.xml
File: app/code/local/Zhlmmc/Helloworld/etc/system.xml
<config>
    <!– … –>
    <sections>
        <!– … –>
        <general>
            <groups>
                <example>
                    <label>Example of Adding a Group</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>1</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>                    
                </example>
            </groups>
        </general>
        <!– … –>
    </section>
</config>

刷新頁麵,你會在“General”標簽頁下麵看到一個新的組,叫做“Example of Adding a Group”。

如何獲得配置數據

到目前為止,我們隻是講了如何設置Magento,可以讓用戶可以配置我們的模塊。現在讓我們來看看如何獲取用戶的配置數據。
Mage::getStoreConfig('helloworld_options/messages/hello_message');
上麵這行代碼就可以獲取我們上麵配置的那個“select”選項的數據。這個函數的參數是我們要獲取的數據的URI,格式如下
section_name/group_name/field_name
你也可以通過以下代碼來獲取一個組或者段的所有值
Mage::getStoreConfig('helloworld_options/messages');
Mage::getStoreConfig('helloworld_options');

最後,如果你想獲取針對某個特定店麵(store)的數據,你可以傳入store ID
Mage::getStoreConfig('helloworld_options',1);

總結

這一章我們講了如何在Magento的後台管理中添加個性化的配置。我們也順便介紹了幫助類的使用和ACL基礎。這裏最重要的內容是後台配置的層級結構,標簽頁包含了配置段,配置段包含了組,組包含了配置選項。我們將在以後的章節中介紹係統配置的高級內容,包括自定義格式,數據驗證等等。

 

 

源文:https://www.zhlmmc.com/?p=674

最後更新:2017-04-02 05:21:05

  上一篇:go ORACLE--SGA之數據緩衝區(Data Buffer)
  下一篇:go magento -- 搜索功能的後台管理技巧二