Magento 模块开发 - Block和Layout

2015年9月3日 Edited 2022年12月6日 浏览量 18 3 min read
Magento 模块开发 - Block和Layout

在上一篇博客中,我们创建了一个简单的模块来输出Hello World,在这篇博客中,我们将通过magento的块和布局来输出Hello World。
我们继续使用之前创建的模块。
现在,配置文件config.xml将有一些改变。由于现在我们使用的是布局文件,我们将在theme/layout文件夹下创建自己的布局文件,并命名为test.xml。

    
        < ?xml version="1.0"? >
        < config >
            < modules >
                < Magease_Test >
                    < version >0.1.0< /version >
                < /Magease_Test >
            < /modules >
            < frontend >
                < routers >
                    < test >
                        < use >standard< /use >
                        < args >
                            < module >Magease_Test< /module >
                            < frontName >test< /frontName >
                        < /args >
                    < /test >
                < /routers >
                < layout >  < !-- 新添加的部分 -- >
                    < updates >
                        < test >
                            < file >test.xml< /file > < !-- 这个模块的布局文件的名字 -- >
                        < /test >
                    < /updates >
                < /layout >
            < /frontend >
            < global >
                < blocks >
                    < test >
                        < class >Magease_Test_Block< /class >
                    < /test >
                < /blocks >
                < helpers >
                    < test >
                        < class >Magease_Test_Helper< /class >
                    < /test >
                < /helpers >
            < /global >
        < /config >
    


现在在主题的布局文件夹下创建test.xml,例如app/design/frontend/default/default/layout/test.xml或者如果你有自己的主题,把布局文件放到那里。这是布局文件的内容。

    
        < ?xml version="1.0"? >
        < layout version="0.1.0" >
            < test_index_index >
                < reference name="content" >
                    < block type="test/test" name="test" template="test/test.phtml" / >
                < /reference >
            < /test_index_index >
        < /layout >
    


这个布局文件的注释之前已经给出了。我们使用的块是"test/test",它等同于类Magease_Test_Block_Test,使用的phtml文件是test/test.phtml。

    
        < ?php echo $this->getContent();? >
    


在这个文件中,调用了一个getContent()函数,这个函数在块文件Test.php文件中声明。像我们之前看到的那样,$this实际上是我们块和类的一个变量,在layout.xml文件中被定义为type。为了确认,把以下代码写到phtml文件中。

    
        < ?php echo get_class($this);? >
    


输出结果应该是Magease_Test_Block_Test。
现在在文件夹app/code/local/Magease/Test/Block下创建文件Test.php。

    
        < ?php 
            class Magease_Test_Block_Test extends Mage_Core_Block_Template
            {
                public function getContent(){
                    echo "Hello World!";
                }
            }
        ? >
    


为了调用所有的布局文件,我们需要对IndexController.php做一些修改。

    
        < ?php 
            class Magease_Test_IndexController extends Mage_Core_Controller_Front_Action
            {
                public function IndexAction(){
                    $this->loadLayout();< !-加载所有的布局文件- >
                    $this_>renderLayout();< !-渲染所有的布局文件- >
                }
            }
        ? >
    


此时,打开URL:www.your-magento.com/index.php/test,会显示Hello World!但这时显示的是3列的布局结构,而不是之前的白色背景。

控制器和URL
在这一部分,将解释一下URL、控制器和方法之间的联系。
magento中任意一个连接都包含三部分,例如:www.your-magento.com/test/index/index,www.your-magento.com/test/view/edit.如果一个URL没有包含这三部分,你就需要自己植入索引,例如:www.your-magento.com/test 实际上等同于 www.your-magento.com/test/index/index,www.your-magento.com/test/view 实际上等同于 www.your-magento.com/test/view/index。
URL这三部分基本包含着<module>/<controller>/<action>,这也同样应用于布局文件。<module>来源于配置文件config.xml中<frontend>给定的值,<controller>是控制器文件的值,比如ViewController.php(view),比如IndexController.php(index)。<action>是控制器文件中函数的名字,例如:indexAction();viewAction();testAction()。
这里有几个例子,使用的是当前的模块。
URL:www.your-magento/test/view/delete
为此,必须创建一个ViewController.php文件

    
        < ?php 
            class Magease_Test_ViewController extends Mage_Core_Controller_Front_Action
            {
                public function deleteAction(){
                    $this->loadLayout();
                    $this->renderLayout();
                }
            }
        ? >
    


另一个URL:www.your-magento.com/test/view_index/xyz,对于这个URL,我们需要在控制器文件夹下创建一个子文件夹,并命名为View,在其中创建IndexController.php文件。

    
        < ?php 
            class Magease_Test_View_IndexController extends Mage_Core_Controller_Front_Action
            {
                public function xyzAction(){
                    $this->loadLayout();
                    $this->renderLayout();
                }
            }
        ? >
    

 

Previous article:
Next article:
Comments
发表评论,留下你的足迹
我们不会公开你的邮箱地址

是否允许我们在发布新内容或者进行促销活动向您发送消息?

Remind me later

Thank you! Please check your email inbox to confirm.

Oops! Notifications are disabled.

© 2014-2023 www.magease.com. All Rights Reserved. 寰云网络 版权所有    鲁ICP备 14014975号-1