12. No More MVC

1. controller responsibility
这里写图片描述
这里写图片描述
这里写图片描述


2.创建一个模块

2.1 配置 module.xml 文件:

app/code/Test/First/etc/module.xml
<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Test_First" setup_version="2.0.0"/>
</config>

2.2 在 config.php 中添加

app/etc/config.php
'Test_First' => 1,

2.3 注册 registration.php

app/code/Test/First/registration.php

<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Test_First',
    __DIR__
);

运行命令。


3.创建控制器
这里写图片描述
3.1创建配置文件

app/code/Test/First/etc/frontend/routes.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="first" frontName="first">
            <module name="Test_First"/>
        </route>
    </router>
</config>

3.2 创建控制器文件
这里写图片描述

<?php

namespace Test\First\Controller\Hello;

class World extends \Magento\Framework\App\Action\Action
{
    public function execute()
    {
        echo "<p>wwww</p>";
        var_dump(__METHOD__);
    }
}

这里写图片描述

这里写图片描述

<?php

namespace Test\First\Controller\Hello;

use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Action\Context;

class World extends \Magento\Framework\App\Action\Action
{
    protected $pageFactory;

    public function __construct(Context $context,PageFactory $pageFactory)
    {
        $this->pageFactory = $pageFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        var_dump(__METHOD__);
        $page_object = $this->pageFactory->create();
        return $page_object;
    }
}

4.创建视图

4.1创建布局文件

这里写图片描述

<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1columns" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="content">
        <block
            template="content.phtml"
            class="Test\First\Block\Main"
            name="test_hello_world"/>
    </referenceBlock>
    </body>
</page>

//注意布局,1columns显示不了

4.2创建 block
这里写图片描述

<?php

namespace Test\First\Block;

use Magento\Framework\View\Element\Template;

class Main extends Template
{
    protected function _prepareLayout()
    {

    } //这一段可有可无
}

4.3创建 phtml

这里写图片描述

<h1>
    hello,world !
</h1>

这里写图片描述
这里写图片描述
这里写图片描述

http://alanstorm.com/magento_2_mvvm_mvc

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值