多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
**修改模板并增加block** 步骤: 1.创建目录文件(以clatalog模块为例) ~~~ app/design/frontend/ ├── <Vendor>/ │ │ ├──...<theme>/ │ │ │ ├── Magento_Catalog │ │ │ | | ├──layout | | | | | | |——catalog_category_view.xml ~~~ ~~~ catalog_category_view.xml(这里的文件名字由于是继承请保持与原来的一致) <?xml version="1.0"?> <!-- /** * Copyright © 2015 Magento. All rights reserved. * See COPYING.txt for license details. */ --> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceBlock name="category.image"> <action method='setTemplate'> <argument name='template' xsi:type='string'>Njzz_Catalog::category/image.phtml</argument> </action> </referenceBlock> </body> </page> name="category.image":你要修改的那个模板的名字; Njzz_Catalog::category/image.phtml:你的模板位置 ~~~ 2.创建目录文件(以clatalog模块为例) ~~~ code/ ├── <Vendor>/ │ │ ├──Clatalog/ │ │ │──Block/Catalog/Category/View.php │ │ │──Controller/ | | |──etc/ | | | |——frontend |——routes.xml | | | |——module.xml |——di.xml | | |——View | | | |——frontend |——templates |——category |——image.phtml | | |——composer.json | | |——registration.php ~~~ 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="catalog" frontName="catalog"> <module name="Njzz_Catalog" /> </route> </router> </config> id="catalog":路由id name="Njzz_Catalog" :配置作用模块 ~~~ module.xml ~~~ <?xml version="1.0"?> <!-- /** * @copyright Copyright (c) 2015 X.commerce, Inc. (http://www.magentocommerce.com) */ --> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Njzz_Catalog" setup_version="2.0.86"> </module> </config> ~~~ image.phtml 模板文件 ~~~ <?php /** * Category view template * * @var $block Njzz\Block\Catalog\Category\View */ ?> <?php $_helper = $this->helper('Magento\Catalog\Helper\Output'); $_category = $block->getCurrentCategory(); $_imgHtml = ''; if ($_imgUrl = $_category->getImageUrl()) { $_imgHtml = '<div class="category-image"><img src="' . $_imgUrl . '" alt="' . $block->escapeHtml($_category->getName()) . '" title="' . $block->escapeHtml($_category->getName()) . '" class="image" /></div>'; $_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image'); /* @escapeNotVerified */ echo $_imgHtml; } $index=$block->index(); ?> /** * Category view template * * @var $block Njzz\Block\Catalog\Category\View:自己在block写的类 命名空间 这样你在image.phtml中才能调 $index=$block->index(); */ ~~~ View.php:block文件 ~~~ <?php /** * Created by PhpStorm. * User: daimingkang * Date: 2016/3/9 * Time: 9:49 */ namespace Njzz\Catalog\Block\Catalog\Category; //这里继承是扩充方法也可以覆盖父类方法 class View extends \Magento\Catalog\Block\Category\View{ public function index(){ var_dump($_SERVER); } } ~~~ di.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:ObjectManager/etc/config.xsd"> <preference for="Magento\Catalog\Block\Category\View" type="Njzz\Catalog\Block\Catalog\Category\View" /> </config> for="Magento\Catalog\Block\Category\View":原来的方法类的命名空间+类名 type="Njzz\Catalog\Block\Catalog\Category\View":现在类的命名空间+类名 ~~~ composer.json ~~~ { "name": "Njzz/Catalog", "description": "N/A", "require": { "php": "~5.5.0|~5.6.0|~7.0.0" }, "type": "magento2-module", "version": "100.0.0", "license": [ "OSL-3.0", "AFL-3.0" ], "autoload": { "files": [ "registration.php" ], "psr-4": { "Njzz\\Catalog\\": "" } } } ~~~ 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, 'Njzz_Catalog',// __DIR__ ); ~~~ 图片: ![](https://box.kancloud.cn/2016-03-09_56df9b4859807.png) ![](https://box.kancloud.cn/2016-03-09_56df9b4aedb7b.png)