Loading categories by their IDs in Magento 2
To load categories by their IDs in Magento 2, Dmitry Fedyuk recommends the following:
magento/magento2/blob/2.0.0/app/code/Magento/Catalog/Controller/Category/View.php#L124
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
* @return \Magento\Catalog\Model\Category */ protected function _initCategory() { $categoryId = (int)$this->getRequest()->getParam('id', false); if (!$categoryId) { return false; } try { $category = $this->categoryRepository->get($categoryId, $this->_storeManager->getStore()->getId()); } catch (NoSuchEntityException $e) { return false; } if (!$this->_objectManager->get('Magento\Catalog\Helper\Category')->canShow($category)) { return false; } $this->_catalogSession->setLastVisitedCategoryId($category->getId()); $this->_coreRegistry->register('current_category', $category); try { $this->_eventManager->dispatch( |