Quantcast
Channel: Magento[マジェント]情報サイト
Viewing all articles
Browse latest Browse all 265

[開発]管理画面へのページ追加 2

$
0
0

上松です。

管理画面へのページ追加について、2回目になります。
コントローラーとブロック、レイアウトについてです。

・コントローラーの設定
config.xmlのメニュー追加時、<action>に’adminhtml/example’と記述したので
Others_Another_Adminhtml_ExampleControllerのindexAction()が呼び出されます。
(indexは省略可能です。’adminhtml/example/index’と書いても同じになります)

app/code/local/Others/Another/etc/config.xml
    <admin>
        <routers>
            <adminhtml> ・・・ メニューの呼び出しで設定したコントローラーのグループ
                <args>
                    <modules>
                        <others_another>Others_Another_Adminhtml</others_another>
                              ・・・ コントローラクラスのプリフィックス
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>

 

app/code/local/Others/Another/controllers/Adiminhtml/ExampleController.php
class Others_Another_Adminhtml_ExampleController extends Mage_Adminhtml_Controller_Action {

    public function indexAction(){
        $this->loadLayout();
        $this->renderLayout();
    }

}

loadLayout()でページレイアウトの読み込み、renderLayou()でHTMLの出力が行われます。
ここまで設定すると、ヘッダとヘッダがついた白い画面になります。

・ブロックの定義
このモジュールで使うブロックグループをconfig.xmlに定義します。

    <global>
...
        <blocks>
            <others_another>
                <class>Others_Another_Block</class>
            </others_another>
        </blocks>
...
    </global>

 

・レイアウトファイルの作成
表示する画面のレイアウトを定義します。

app/design/adminhtml/default/default/layout/othres_another.xml
<?xml version="1.0"?>
<layout version="1.0.0">

    <adminhtml_example_index> ・・・ 表示する画面のアクション
        <reference name="content"> ・・・ 表示場所
            <block type="others_another/adminhtml_example" name="example"/> ・・・ ブロック
        </reference>
    </adminhtml_example_index>

</layout>

作成したレイアウトファイルを読み込まれるようconfig.xmlへ定義します。

...
    <adminhtml>
...
        <layout>
            <updates>
                <others_another>
                    <file>others_another.xml</file>
                </others_another>
            </updates>
        </layout>
...

 

・ブロックの作成
定義したブロック実際に作ります。
ここではGridを利用して一覧を表示させます。

Gridコンテナ
app/code/local/Others/Another/Block/Adminhtml/Example.php

class Others_Another_Block_Adminhtml_Example extends Mage_Adminhtml_Block_Widget_Grid_Container {

    public function __construct() {
        parent::__construct(); ・・・ 親クラスのメソッド呼び出し
        $this->_headerText = Mage::helper('others_another_hp')->__('Examples'); 
            ・・・ 一覧上部に表示する文字列
        $this->_controller = 'adminhtml_example'; 
            ・・・ コントローラ名
        $this->_blockGroup = 'others_another'; 
            ・・・ ブロックグループ名
    }

}

親クラスの__construct()メソッド内では、テンプレートと「Add New」のボタンが設定されるます。
$this->_headerTextで設定している値は一覧のタイトル位置に表示される文字列になります。
$this->_controllerと$this->_blockGroupはGridクラスを指定するのに使用します。
親クラスの_prepareLayout()の中で

(ブロックグループ)/$_controller.'_grid'
の形でGridクラスが指定されます。
内部では以下の形でGridクラスのインスタンスを生成します。
(config.xmlで指定したブロックグループの)_($_controllerで指定した文字列)_Grid'

今回の例だと’Others_Another_Block_Adminhtml_Example_Grid’という形になります。

Gridクラス
app/code/local/Others/Another/Block/Adminhtml/Example/Grid.php

class Others_Another_Block_Adminhtml_Example_Grid extends Mage_Adminhtml_Block_Widget_Grid {
}

とりあえずクラスだけを用意します。 (クラス名などはGridコンテナで設定した値と合わせておく必要があります。)

ここまで設定すると
一覧のタイトルと「Add New」ボタンが画面に表示されるようになります。


次回以降で、実際に一覧画面の表示についてまとめようと思います。


Viewing all articles
Browse latest Browse all 265

Trending Articles