【问题标题】:How to return module setup_version in Magento 2 controller?如何在 Magento 2 控制器中返回模块 setup_version?
【发布时间】:2021-12-03 10:09:07
【问题描述】:

我有一个 magento 扩展,我想在其中一个控制器中显示 setup_version。

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

在控制器内部:

<?php

namespace My\Module\Controller\Index;

use Magento\Framework\App\Config\ScopeConfigInterface;

class Version extends AbstractIndex
{
    /**
     * @var ScopeConfigInterface
     */
    protected $storeConfig;

    public function __construct(ScopeConfigInterface $storeConfig)
    {
        $this->storeConfig = $storeConfig;

    }

   public function execute() {
      // just prints the version
      echo (string)$this->storeConfig->getValue('modules/My_Module/setup_version');
   }
}

但显然我确实得到了一个错误:

错误:在 /home/alexandru/magento24/vendor/magento/framework/App/Action/Action.php:109 中的 null 上调用成员函数 get()

【问题讨论】:

    标签: magento2


    【解决方案1】:
    <?php
    namespace Module\Name\Model\Data;
    use Magento\Framework\App\ProductMetadataInterface;
    class NameFile
    {
        protected $productMetadata;
        public function __construct(
            ProductMetadataInterface $productMetadata
        ) {
            $this->productMetadata = $productMetadata;
        }
        public function getVersion()
        {
            $magentoVersion = $this->productMetadata->getVersion();
            return $magentoVersion;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 2011-03-21
      • 2012-12-17
      • 1970-01-01
      相关资源
      最近更新 更多