自定义块 Magento 2 不工作
Custom Block Magento 2 isnt't working
我想给 \Magento\Catalog\Block\Product\View
添加一个新功能
我做了什么:
1. app/code/Company/Module
composer.json
{
"name": "Company/Module",
"description": "N/A",
"require": {
"php": "~5.5.0|~5.6.0|~7.0.0"
},
"type": "magento2-module",
"version": "100.0.2",
"license": [
"OSL-3.0",
"AFL-3.0"
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Magento\Cms\": ""
}
}
}
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,
'Company_Module',
__DIR__
);
2. app/code/Company/Module/Product/View
此文件内:
<?php
namespace Company\Module\Block\Product;
class View extends \Magento\Catalog\Block\Product\View {
private function trySomething()
{
exit('test');return '123';
}
}
?>
3. app/code/Company/Module/etc
在此文件夹内:
di.xml
内容:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Catalog\Block\Product\View" type="Company\Module\Block\Product\View" />
</config>
也在文件夹中:
module.xml
内容:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Company_Module" setup_version="1.0.0">
</module>
</config>
我想通过模板调用函数,像这样包含在 product_detail 中:
/**
* Product view template
*
* @var $block \Company\Module\Block\Product\View
*/
?>
<?php echo $block->trySomething(); ?>
This is just another test
文本 "This is just another test" 在 product_detail 上正确显示,但函数不会被调用。
希望有人能帮助我。
我自己解决了,问题是块和布局不在同一个模块中。我在 app/code/local/Company/Module
下创建了 view/frontend/layout
和 view/frontend/templates
这解决了它 4 我。
我想给 \Magento\Catalog\Block\Product\View
我做了什么:
1. app/code/Company/Module
composer.json
{
"name": "Company/Module",
"description": "N/A",
"require": {
"php": "~5.5.0|~5.6.0|~7.0.0"
},
"type": "magento2-module",
"version": "100.0.2",
"license": [
"OSL-3.0",
"AFL-3.0"
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Magento\Cms\": ""
}
}
}
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,
'Company_Module',
__DIR__
);
2. app/code/Company/Module/Product/View
此文件内:
<?php
namespace Company\Module\Block\Product;
class View extends \Magento\Catalog\Block\Product\View {
private function trySomething()
{
exit('test');return '123';
}
}
?>
3. app/code/Company/Module/etc
在此文件夹内:
di.xml
内容:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Catalog\Block\Product\View" type="Company\Module\Block\Product\View" />
</config>
也在文件夹中:
module.xml
内容:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Company_Module" setup_version="1.0.0">
</module>
</config>
我想通过模板调用函数,像这样包含在 product_detail 中:
/**
* Product view template
*
* @var $block \Company\Module\Block\Product\View
*/
?>
<?php echo $block->trySomething(); ?>
This is just another test
文本 "This is just another test" 在 product_detail 上正确显示,但函数不会被调用。
希望有人能帮助我。
我自己解决了,问题是块和布局不在同一个模块中。我在 app/code/local/Company/Module
下创建了 view/frontend/layout
和 view/frontend/templates
这解决了它 4 我。