【问题标题】:How to get current category in magento2?如何在 magento2 中获取当前类别?
【发布时间】:2016-04-25 19:33:17
【问题描述】:

如何在 ma​​gento2 中获取当前类别?

我想在自定义 phtml 文件中获取类别名称和类别 ID。

【问题讨论】:

  • 这里的大多数答案都使用Registry 类,该类已被弃用一段时间。有没有办法不用Registry

标签: magento2 magento-2.0


【解决方案1】:

在Category页面的*.phtml文件中可以通过sn-p获取Category数据:

$currentCategory = $this->helper('Magento\Catalog\Helper\Data')->getCategory();

【讨论】:

    【解决方案2】:

    上面看起来是正确的,但我认为直接跳转到注册表不是最好的方法。 Magento 提供了一个已经封装了该功能的层解析器。 (参见目录插件中的 TopMenu 块)

    我建议注入 \Magento\Catalog\Model\Layer\Resolver 类并使用它来获取当前类别。这是代码:

    <?php
    
    namespace FooBar\Demo\Block;
    
    class Demo extends \Magento\Framework\View\Element\Template
    {
        private $layerResolver;
    
        public function __construct(
            \Magento\Framework\View\Element\Template\Context $context,
            \Magento\Catalog\Model\Layer\Resolver $layerResolver,
            array $data = []
        ) {
            parent::__construct($context, $data);
    
            $this->layerResolver = $layerResolver;
        }
    
        public function getCurrentCategory()
        {
            return $this->layerResolver->get()->getCurrentCategory();
        }
    }
    

    下面是实际的 getCurrentCategory() 方法在 Resolver 类中的作用。

    public function getCurrentCategory()
    {
        $category = $this->getData('current_category');
        if ($category === null) {
            $category = $this->registry->registry('current_category');
            if ($category) {
                $this->setData('current_category', $category);
            } else {
                $category = $this->categoryRepository->get($this->getCurrentStore()->getRootCategoryId());
                $this->setData('current_category', $category);
            }
        }
    
        return $category;
    }
    

    如您所见,它仍然使用注册表,但在失败时提供备用。

    【讨论】:

    • +1 因为没有重新发明轮子!这不是 OP 所要求的完全,但由于回退机制,它可能是比其他答案更好的解决方案。我知道这是给我的。
    【解决方案3】:

    无需使用对象管理器或注入类。您可以通过以下方式使用内置帮助器类Magento\Catalog\Helper\Data

    <?php 
        $catalogHelperData = $this->helper('Magento\Catalog\Helper\Data');
        $categoryObject = $catalogHelperData->getCategory();
        $categoryId = $categoryObject->getId();
        $categoryName = $categoryObject->getName();
    ?>
    

    此代码 sn-p 应适用于与产品列表页面或产品详细信息页面相关的任何 phtml内置或自定义)文件。

    【讨论】:

      【解决方案4】:

      Magento 为正在访问的类别设置注册表。因此,要获取当前类别,请使用以下方法:

      /**
       * @param \Magento\Framework\Registry $registry
       */
      
      protected $_registry;
      
      public function __construct(
          \Magento\Framework\Registry $registry
      ) {
          $this->_registry = $registry;
      }
      

      然后使用:

      $category = $this->_registry->registry('current_category');//get current category
      

      现在您可以访问集合并获取详细信息,例如 $category->getName()

      【讨论】:

      • 这是正确的回复,因为与上面选择的不同,此代码尊重依赖注入原则,并且不要在代码中直接使用 objectManager
      • 我同意,在 Magento 2 中使用 ObjectManager 是不好的做法
      • 谢谢@Sarfaraj
      • @ManishJoy 注册表已弃用,您能否提出新的解决方案?
      • 这是它的替代品atwix.com/magento-2/…
      【解决方案5】:

      试试这个代码。这肯定会对你有所帮助。

      <?php 
          $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
          $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
          echo $category->getId();
          echo $category->getName();
      ?>
      

      【讨论】:

      • 不遵守依赖注入原则
      • 有什么理由将我的评论标记出来? @Makwana,您应该更好地从错误中吸取教训并分享良好做法(也许可以解决您的答案),而不是像这样欺负人
      • 我同意@YonnTrimoreau 你应该遵循依赖注入原则。直接使用对象管理器是一种不好的做法
      • 是的,像这样直接使用 OM 真的很糟糕。来自official Magento dev docs - Magento 禁止在您的代码中直接使用 ObjectManager,因为它隐藏了类的真正依赖关系。
      • 好吧,它会正常工作的。但是在 Magento 中使用 objectManager 并不受欢迎。
      猜你喜欢
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      • 1970-01-01
      • 1970-01-01
      • 2012-06-27
      • 2016-12-03
      • 1970-01-01
      相关资源
      最近更新 更多