Magento 2 : How to write controller to load category page?I created a custom module ,but getting error, not able to figure out what the error is about. How to get out of this error?Magento 2.1: Invoke urlBuilder->getUrl() in a controller in a custom moduleMagento 1 category page show subcategories before productsSomething went wrong while saving the category in Magento 2.1.9Form is not displayed on panel admin Magento 2Not able to add/update magento2 category post upgrade from 1.9 to 2.1.0magento 2 Deepest Category product list page gives Error 500. Which files to inspect?Magento2.3 : Layered Navigation issueHow to filter category collection?For heavier category(having 2000 products) ,category page takes 4-5 min to load, while for a small category( few products) it's working properly
Is it a problem if <h4>, <h5> and <h6> are smaller than regular text?
Thread Pool C++ Implementation
What is wrong with this proof that symmetric matrices commute?
Character descriptions
Mobile App Appraisal
Is the term 'open source' a trademark?
How do governments keep track of their issued currency?
Pre-1972 sci-fi short story or novel: alien(?) tunnel where people try new moves and get destroyed if they're not the correct ones
Motivation - or how can I get myself to do the work I know I need to?
Difference between > and >> when used with a named pipe
English word for "product of tinkering"
Frame failure sudden death?
Should an arbiter claim draw at a K+R vs K+R endgame?
Where Mongol herds graze
Overlapping String-Blocks
Soft question: Examples where lack of mathematical rigour cause security breaches?
Can I make plugins required?
Were Alexander the Great and Hephaestion lovers?
How to construct an hbox with negative height?
Passing multiple files through stdin (over ssh)
Cycle through MeshStyle directives in ListLinePlot
Arriving at the same result with the opposite hypotheses
How can I get an unreasonable manager to approve time off?
Why doesn't Adrian Toomes give up Spider-Man's identity?
Magento 2 : How to write controller to load category page?
I created a custom module ,but getting error, not able to figure out what the error is about. How to get out of this error?Magento 2.1: Invoke urlBuilder->getUrl() in a controller in a custom moduleMagento 1 category page show subcategories before productsSomething went wrong while saving the category in Magento 2.1.9Form is not displayed on panel admin Magento 2Not able to add/update magento2 category post upgrade from 1.9 to 2.1.0magento 2 Deepest Category product list page gives Error 500. Which files to inspect?Magento2.3 : Layered Navigation issueHow to filter category collection?For heavier category(having 2000 products) ,category page takes 4-5 min to load, while for a small category( few products) it's working properly
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have customized a Layered navigation modules in Magento 2. However, I can't write a controller to get all content of category page and load into controller page(localhost/magento2/layerednavigation/index/index/id/17?cat=15&style_general=117
).
The controller page will load but it don't filler product. I have checked and seen that controller page don't get param(cat, style_general....).
Summary, I want write controller to load category page. However, I am facing the issue is the filler don't work into controller page.
Please refer code:
1. Code for layout: layerednavigation_index_index.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2016 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>
<referenceContainer name="columns.top">
<container name="category.view.container" htmlTag="div" htmlClass="category-view" after="-">
<block class="MagentoCatalogBlockCategoryView" name="category.image" template="Magento_Catalog::category/image.phtml"/>
<block class="MagentoCatalogBlockCategoryView" name="category.description" template="Magento_Catalog::category/description.phtml"/>
<block class="MagentoCatalogBlockCategoryView" name="category.cms" template="Magento_Catalog::category/cms.phtml"/>
</container>
</referenceContainer>
<referenceContainer name="content">
<block class="MagentoCatalogBlockCategoryView" name="category.products" template="Magento_Catalog::category/products.phtml">
<block class="MagentoCatalogBlockProductListProduct" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">
<container name="category.product.list.additional" as="additional" />
<block class="MagentoFrameworkViewElementRendererList" name="category.product.type.details.renderers" as="details.renderers">
<block class="MagentoFrameworkViewElementTemplate" as="default"/>
</block>
<block class="MagentoCatalogBlockProductProductListToolbar" name="product_list_toolbar" template="Magento_Catalog::product/list/toolbar.phtml">
<block class="MagentoThemeBlockHtmlPager" name="product_list_toolbar_pager"/>
<!-- The following code shows how to set your own pager increments -->
<!--
<action method="setDefaultListPerPage">
<argument name="limit" xsi:type="string">4</argument>
</action>
<action method="setDefaultGridPerPage">
<argument name="limit" xsi:type="string">3</argument>
</action>
<action method="addPagerLimit">
<argument name="mode" xsi:type="string">list</argument>
<argument name="limit" xsi:type="string">2</argument>
</action>
<action method="addPagerLimit">
<argument name="mode" xsi:type="string">list</argument>
<argument name="limit" xsi:type="string">4</argument>
</action>
<action method="addPagerLimit">
<argument name="mode" xsi:type="string">list</argument>
<argument name="limit" xsi:type="string">6</argument>
</action>
<action method="addPagerLimit">
<argument name="mode" xsi:type="string">list</argument>
<argument name="limit" xsi:type="string">8</argument>
</action>
<action method="addPagerLimit" translate="label">
<argument name="mode" xsi:type="string">list</argument>
<argument name="limit" xsi:type="string">all</argument>
<argument name="label" xsi:type="string">All</argument>
</action>
<action method="addPagerLimit">
<argument name="mode" xsi:type="string">grid</argument>
<argument name="limit" xsi:type="string">3</argument>
</action>
<action method="addPagerLimit">
<argument name="mode" xsi:type="string">grid</argument>
<argument name="limit" xsi:type="string">6</argument>
</action>
<action method="addPagerLimit">
<argument name="mode" xsi:type="string">grid</argument>
<argument name="limit" xsi:type="string">9</argument>
</action>
<action method="addPagerLimit" translate="label">
<argument name="mode" xsi:type="string">grid</argument>
<argument name="limit" xsi:type="string">all</argument>
<argument name="label" xsi:type="string">All</argument>
</action>
-->
</block>
<action method="setToolbarBlockName">
<argument name="name" xsi:type="string">product_list_toolbar</argument>
</action>
</block>
</block>
<block class="MagentoCookieBlockRequireCookie" name="require-cookie" template="Magento_Cookie::require_cookie.phtml">
<arguments>
<argument name="triggers" xsi:type="array">
<item name="compareProductLink" xsi:type="string">.action.tocompare</item>
</argument>
</arguments>
</block>
</referenceContainer>
<referenceBlock name="page.main.title">
<arguments>
<argument name="id" xsi:type="string">page-title-heading</argument>
<argument name="add_base_attribute_aria" xsi:type="string">page-title-heading toolbar-amount</argument>
</arguments>
<block class="MagentoCatalogBlockCategoryRssLink" name="rss.link" template="Magento_Catalog::category/rss.phtml"/>
</referenceBlock>
</body>
</page>
Controller code: index.php
<?php
namespace EmthemesLayeredNavigationProControllerIndex;
use MagentoCatalogApiCategoryRepositoryInterface;
use MagentoCatalogModelLayerResolver;
use MagentoFrameworkExceptionNoSuchEntityException;
use MagentoFrameworkViewResultPageFactory;
class Index extends MagentoFrameworkAppActionAction
protected $_coreRegistry = null;
protected $_catalogSession;
protected $_catalogDesign;
protected $_storeManager;
protected $categoryUrlPathGenerator;
protected $resultPageFactory;
protected $resultForwardFactory;
private $layerResolver;
protected $categoryRepository;
/**
* Constructor
*
* @param MagentoFrameworkAppActionContext $context
* @param MagentoCatalogModelDesign $catalogDesign
* @param MagentoCatalogModelSession $catalogSession
* @param MagentoFrameworkRegistry $coreRegistry
* @param MagentoStoreModelStoreManagerInterface $storeManager
* @param MagentoCatalogUrlRewriteModelCategoryUrlPathGenerator $categoryUrlPathGenerator
* @param MagentoFrameworkViewResultPageFactory $resultPageFactory
* @param MagentoFrameworkControllerResultForwardFactory $resultForwardFactory
* @param Resolver $layerResolver
* @param CategoryRepositoryInterface $categoryRepository
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoCatalogModelDesign $catalogDesign,
MagentoCatalogModelSession $catalogSession,
MagentoFrameworkRegistry $coreRegistry,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoCatalogUrlRewriteModelCategoryUrlPathGenerator $categoryUrlPathGenerator,
PageFactory $resultPageFactory,
MagentoFrameworkControllerResultForwardFactory $resultForwardFactory,
Resolver $layerResolver,
CategoryRepositoryInterface $categoryRepository
)
parent::__construct($context);
$this->_storeManager = $storeManager;
$this->_catalogDesign = $catalogDesign;
$this->_catalogSession = $catalogSession;
$this->_coreRegistry = $coreRegistry;
$this->categoryUrlPathGenerator = $categoryUrlPathGenerator;
$this->resultPageFactory = $resultPageFactory;
$this->resultForwardFactory = $resultForwardFactory;
$this->layerResolver = $layerResolver;
$this->categoryRepository = $categoryRepository;
/**
* Initialize requested category object
*
* @return MagentoCatalogModelCategory
*/
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('MagentoCatalogHelperCategory')->canShow($category))
return false;
$this->_catalogSession->setLastVisitedCategoryId($category->getId());
$this->_coreRegistry->register('current_category', $category);
try
$this->_eventManager->dispatch(
'catalog_controller_category_init_after',
['category' => $category, 'controller_action' => $this]
);
catch (MagentoFrameworkExceptionLocalizedException $e)
$this->_objectManager->get('PsrLogLoggerInterface')->critical($e);
return false;
return $category;
/**
* Category view action
*
* @return MagentoFrameworkControllerResultInterface
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function execute()
if ($this->_request->getParam(MagentoFrameworkAppActionInterface::PARAM_NAME_URL_ENCODED))
return $this->resultRedirectFactory->create()->setUrl($this->_redirect->getRedirectUrl());
$category = $this->_initCategory();
if ($category)
$this->layerResolver->create(Resolver::CATALOG_LAYER_CATEGORY);
$settings = $this->_catalogDesign->getDesignSettings($category);
// apply custom design
if ($settings->getCustomDesign())
$this->_catalogDesign->applyCustomDesign($settings->getCustomDesign());
$this->_catalogSession->setLastViewedCategoryId($category->getId());
$page = $this->resultPageFactory->create();
// apply custom layout (page) template once the blocks are generated
if ($settings->getPageLayout())
$page->getConfig()->setPageLayout($settings->getPageLayout());
$hasChildren = $category->hasChildren();
if ($category->getIsAnchor())
$type = $hasChildren ? 'layered' : 'layered_without_children';
else
$type = $hasChildren ? 'default' : 'default_without_children';
if (!$hasChildren)
// Two levels removed from parent. Need to add default page type.
$parentType = strtok($type, '_');
$page->addPageLayoutHandles(['type' => $parentType]);
$page->addPageLayoutHandles(['type' => $type, 'id' => $category->getId()]);
// apply custom layout update once layout is loaded
$layoutUpdates = $settings->getLayoutUpdates();
if ($layoutUpdates && is_array($layoutUpdates))
foreach ($layoutUpdates as $layoutUpdate)
$page->addUpdate($layoutUpdate);
$page->addPageLayoutHandles(['layout_update' => md5($layoutUpdate)]);
$page->getConfig()->addBodyClass('page-products')
->addBodyClass('categorypath-' . $this->categoryUrlPathGenerator->getUrlPath($category))
->addBodyClass('category-' . $category->getUrlKey());
$block = $page->getLayout()->getBlock('category.products');
$this->getResponse()->setBody($block->toHtml());
category magento-2.1 layered-navigation category-products category-view
add a comment |
I have customized a Layered navigation modules in Magento 2. However, I can't write a controller to get all content of category page and load into controller page(localhost/magento2/layerednavigation/index/index/id/17?cat=15&style_general=117
).
The controller page will load but it don't filler product. I have checked and seen that controller page don't get param(cat, style_general....).
Summary, I want write controller to load category page. However, I am facing the issue is the filler don't work into controller page.
Please refer code:
1. Code for layout: layerednavigation_index_index.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2016 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>
<referenceContainer name="columns.top">
<container name="category.view.container" htmlTag="div" htmlClass="category-view" after="-">
<block class="MagentoCatalogBlockCategoryView" name="category.image" template="Magento_Catalog::category/image.phtml"/>
<block class="MagentoCatalogBlockCategoryView" name="category.description" template="Magento_Catalog::category/description.phtml"/>
<block class="MagentoCatalogBlockCategoryView" name="category.cms" template="Magento_Catalog::category/cms.phtml"/>
</container>
</referenceContainer>
<referenceContainer name="content">
<block class="MagentoCatalogBlockCategoryView" name="category.products" template="Magento_Catalog::category/products.phtml">
<block class="MagentoCatalogBlockProductListProduct" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">
<container name="category.product.list.additional" as="additional" />
<block class="MagentoFrameworkViewElementRendererList" name="category.product.type.details.renderers" as="details.renderers">
<block class="MagentoFrameworkViewElementTemplate" as="default"/>
</block>
<block class="MagentoCatalogBlockProductProductListToolbar" name="product_list_toolbar" template="Magento_Catalog::product/list/toolbar.phtml">
<block class="MagentoThemeBlockHtmlPager" name="product_list_toolbar_pager"/>
<!-- The following code shows how to set your own pager increments -->
<!--
<action method="setDefaultListPerPage">
<argument name="limit" xsi:type="string">4</argument>
</action>
<action method="setDefaultGridPerPage">
<argument name="limit" xsi:type="string">3</argument>
</action>
<action method="addPagerLimit">
<argument name="mode" xsi:type="string">list</argument>
<argument name="limit" xsi:type="string">2</argument>
</action>
<action method="addPagerLimit">
<argument name="mode" xsi:type="string">list</argument>
<argument name="limit" xsi:type="string">4</argument>
</action>
<action method="addPagerLimit">
<argument name="mode" xsi:type="string">list</argument>
<argument name="limit" xsi:type="string">6</argument>
</action>
<action method="addPagerLimit">
<argument name="mode" xsi:type="string">list</argument>
<argument name="limit" xsi:type="string">8</argument>
</action>
<action method="addPagerLimit" translate="label">
<argument name="mode" xsi:type="string">list</argument>
<argument name="limit" xsi:type="string">all</argument>
<argument name="label" xsi:type="string">All</argument>
</action>
<action method="addPagerLimit">
<argument name="mode" xsi:type="string">grid</argument>
<argument name="limit" xsi:type="string">3</argument>
</action>
<action method="addPagerLimit">
<argument name="mode" xsi:type="string">grid</argument>
<argument name="limit" xsi:type="string">6</argument>
</action>
<action method="addPagerLimit">
<argument name="mode" xsi:type="string">grid</argument>
<argument name="limit" xsi:type="string">9</argument>
</action>
<action method="addPagerLimit" translate="label">
<argument name="mode" xsi:type="string">grid</argument>
<argument name="limit" xsi:type="string">all</argument>
<argument name="label" xsi:type="string">All</argument>
</action>
-->
</block>
<action method="setToolbarBlockName">
<argument name="name" xsi:type="string">product_list_toolbar</argument>
</action>
</block>
</block>
<block class="MagentoCookieBlockRequireCookie" name="require-cookie" template="Magento_Cookie::require_cookie.phtml">
<arguments>
<argument name="triggers" xsi:type="array">
<item name="compareProductLink" xsi:type="string">.action.tocompare</item>
</argument>
</arguments>
</block>
</referenceContainer>
<referenceBlock name="page.main.title">
<arguments>
<argument name="id" xsi:type="string">page-title-heading</argument>
<argument name="add_base_attribute_aria" xsi:type="string">page-title-heading toolbar-amount</argument>
</arguments>
<block class="MagentoCatalogBlockCategoryRssLink" name="rss.link" template="Magento_Catalog::category/rss.phtml"/>
</referenceBlock>
</body>
</page>
Controller code: index.php
<?php
namespace EmthemesLayeredNavigationProControllerIndex;
use MagentoCatalogApiCategoryRepositoryInterface;
use MagentoCatalogModelLayerResolver;
use MagentoFrameworkExceptionNoSuchEntityException;
use MagentoFrameworkViewResultPageFactory;
class Index extends MagentoFrameworkAppActionAction
protected $_coreRegistry = null;
protected $_catalogSession;
protected $_catalogDesign;
protected $_storeManager;
protected $categoryUrlPathGenerator;
protected $resultPageFactory;
protected $resultForwardFactory;
private $layerResolver;
protected $categoryRepository;
/**
* Constructor
*
* @param MagentoFrameworkAppActionContext $context
* @param MagentoCatalogModelDesign $catalogDesign
* @param MagentoCatalogModelSession $catalogSession
* @param MagentoFrameworkRegistry $coreRegistry
* @param MagentoStoreModelStoreManagerInterface $storeManager
* @param MagentoCatalogUrlRewriteModelCategoryUrlPathGenerator $categoryUrlPathGenerator
* @param MagentoFrameworkViewResultPageFactory $resultPageFactory
* @param MagentoFrameworkControllerResultForwardFactory $resultForwardFactory
* @param Resolver $layerResolver
* @param CategoryRepositoryInterface $categoryRepository
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoCatalogModelDesign $catalogDesign,
MagentoCatalogModelSession $catalogSession,
MagentoFrameworkRegistry $coreRegistry,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoCatalogUrlRewriteModelCategoryUrlPathGenerator $categoryUrlPathGenerator,
PageFactory $resultPageFactory,
MagentoFrameworkControllerResultForwardFactory $resultForwardFactory,
Resolver $layerResolver,
CategoryRepositoryInterface $categoryRepository
)
parent::__construct($context);
$this->_storeManager = $storeManager;
$this->_catalogDesign = $catalogDesign;
$this->_catalogSession = $catalogSession;
$this->_coreRegistry = $coreRegistry;
$this->categoryUrlPathGenerator = $categoryUrlPathGenerator;
$this->resultPageFactory = $resultPageFactory;
$this->resultForwardFactory = $resultForwardFactory;
$this->layerResolver = $layerResolver;
$this->categoryRepository = $categoryRepository;
/**
* Initialize requested category object
*
* @return MagentoCatalogModelCategory
*/
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('MagentoCatalogHelperCategory')->canShow($category))
return false;
$this->_catalogSession->setLastVisitedCategoryId($category->getId());
$this->_coreRegistry->register('current_category', $category);
try
$this->_eventManager->dispatch(
'catalog_controller_category_init_after',
['category' => $category, 'controller_action' => $this]
);
catch (MagentoFrameworkExceptionLocalizedException $e)
$this->_objectManager->get('PsrLogLoggerInterface')->critical($e);
return false;
return $category;
/**
* Category view action
*
* @return MagentoFrameworkControllerResultInterface
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function execute()
if ($this->_request->getParam(MagentoFrameworkAppActionInterface::PARAM_NAME_URL_ENCODED))
return $this->resultRedirectFactory->create()->setUrl($this->_redirect->getRedirectUrl());
$category = $this->_initCategory();
if ($category)
$this->layerResolver->create(Resolver::CATALOG_LAYER_CATEGORY);
$settings = $this->_catalogDesign->getDesignSettings($category);
// apply custom design
if ($settings->getCustomDesign())
$this->_catalogDesign->applyCustomDesign($settings->getCustomDesign());
$this->_catalogSession->setLastViewedCategoryId($category->getId());
$page = $this->resultPageFactory->create();
// apply custom layout (page) template once the blocks are generated
if ($settings->getPageLayout())
$page->getConfig()->setPageLayout($settings->getPageLayout());
$hasChildren = $category->hasChildren();
if ($category->getIsAnchor())
$type = $hasChildren ? 'layered' : 'layered_without_children';
else
$type = $hasChildren ? 'default' : 'default_without_children';
if (!$hasChildren)
// Two levels removed from parent. Need to add default page type.
$parentType = strtok($type, '_');
$page->addPageLayoutHandles(['type' => $parentType]);
$page->addPageLayoutHandles(['type' => $type, 'id' => $category->getId()]);
// apply custom layout update once layout is loaded
$layoutUpdates = $settings->getLayoutUpdates();
if ($layoutUpdates && is_array($layoutUpdates))
foreach ($layoutUpdates as $layoutUpdate)
$page->addUpdate($layoutUpdate);
$page->addPageLayoutHandles(['layout_update' => md5($layoutUpdate)]);
$page->getConfig()->addBodyClass('page-products')
->addBodyClass('categorypath-' . $this->categoryUrlPathGenerator->getUrlPath($category))
->addBodyClass('category-' . $category->getUrlKey());
$block = $page->getLayout()->getBlock('category.products');
$this->getResponse()->setBody($block->toHtml());
category magento-2.1 layered-navigation category-products category-view
add a comment |
I have customized a Layered navigation modules in Magento 2. However, I can't write a controller to get all content of category page and load into controller page(localhost/magento2/layerednavigation/index/index/id/17?cat=15&style_general=117
).
The controller page will load but it don't filler product. I have checked and seen that controller page don't get param(cat, style_general....).
Summary, I want write controller to load category page. However, I am facing the issue is the filler don't work into controller page.
Please refer code:
1. Code for layout: layerednavigation_index_index.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2016 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>
<referenceContainer name="columns.top">
<container name="category.view.container" htmlTag="div" htmlClass="category-view" after="-">
<block class="MagentoCatalogBlockCategoryView" name="category.image" template="Magento_Catalog::category/image.phtml"/>
<block class="MagentoCatalogBlockCategoryView" name="category.description" template="Magento_Catalog::category/description.phtml"/>
<block class="MagentoCatalogBlockCategoryView" name="category.cms" template="Magento_Catalog::category/cms.phtml"/>
</container>
</referenceContainer>
<referenceContainer name="content">
<block class="MagentoCatalogBlockCategoryView" name="category.products" template="Magento_Catalog::category/products.phtml">
<block class="MagentoCatalogBlockProductListProduct" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">
<container name="category.product.list.additional" as="additional" />
<block class="MagentoFrameworkViewElementRendererList" name="category.product.type.details.renderers" as="details.renderers">
<block class="MagentoFrameworkViewElementTemplate" as="default"/>
</block>
<block class="MagentoCatalogBlockProductProductListToolbar" name="product_list_toolbar" template="Magento_Catalog::product/list/toolbar.phtml">
<block class="MagentoThemeBlockHtmlPager" name="product_list_toolbar_pager"/>
<!-- The following code shows how to set your own pager increments -->
<!--
<action method="setDefaultListPerPage">
<argument name="limit" xsi:type="string">4</argument>
</action>
<action method="setDefaultGridPerPage">
<argument name="limit" xsi:type="string">3</argument>
</action>
<action method="addPagerLimit">
<argument name="mode" xsi:type="string">list</argument>
<argument name="limit" xsi:type="string">2</argument>
</action>
<action method="addPagerLimit">
<argument name="mode" xsi:type="string">list</argument>
<argument name="limit" xsi:type="string">4</argument>
</action>
<action method="addPagerLimit">
<argument name="mode" xsi:type="string">list</argument>
<argument name="limit" xsi:type="string">6</argument>
</action>
<action method="addPagerLimit">
<argument name="mode" xsi:type="string">list</argument>
<argument name="limit" xsi:type="string">8</argument>
</action>
<action method="addPagerLimit" translate="label">
<argument name="mode" xsi:type="string">list</argument>
<argument name="limit" xsi:type="string">all</argument>
<argument name="label" xsi:type="string">All</argument>
</action>
<action method="addPagerLimit">
<argument name="mode" xsi:type="string">grid</argument>
<argument name="limit" xsi:type="string">3</argument>
</action>
<action method="addPagerLimit">
<argument name="mode" xsi:type="string">grid</argument>
<argument name="limit" xsi:type="string">6</argument>
</action>
<action method="addPagerLimit">
<argument name="mode" xsi:type="string">grid</argument>
<argument name="limit" xsi:type="string">9</argument>
</action>
<action method="addPagerLimit" translate="label">
<argument name="mode" xsi:type="string">grid</argument>
<argument name="limit" xsi:type="string">all</argument>
<argument name="label" xsi:type="string">All</argument>
</action>
-->
</block>
<action method="setToolbarBlockName">
<argument name="name" xsi:type="string">product_list_toolbar</argument>
</action>
</block>
</block>
<block class="MagentoCookieBlockRequireCookie" name="require-cookie" template="Magento_Cookie::require_cookie.phtml">
<arguments>
<argument name="triggers" xsi:type="array">
<item name="compareProductLink" xsi:type="string">.action.tocompare</item>
</argument>
</arguments>
</block>
</referenceContainer>
<referenceBlock name="page.main.title">
<arguments>
<argument name="id" xsi:type="string">page-title-heading</argument>
<argument name="add_base_attribute_aria" xsi:type="string">page-title-heading toolbar-amount</argument>
</arguments>
<block class="MagentoCatalogBlockCategoryRssLink" name="rss.link" template="Magento_Catalog::category/rss.phtml"/>
</referenceBlock>
</body>
</page>
Controller code: index.php
<?php
namespace EmthemesLayeredNavigationProControllerIndex;
use MagentoCatalogApiCategoryRepositoryInterface;
use MagentoCatalogModelLayerResolver;
use MagentoFrameworkExceptionNoSuchEntityException;
use MagentoFrameworkViewResultPageFactory;
class Index extends MagentoFrameworkAppActionAction
protected $_coreRegistry = null;
protected $_catalogSession;
protected $_catalogDesign;
protected $_storeManager;
protected $categoryUrlPathGenerator;
protected $resultPageFactory;
protected $resultForwardFactory;
private $layerResolver;
protected $categoryRepository;
/**
* Constructor
*
* @param MagentoFrameworkAppActionContext $context
* @param MagentoCatalogModelDesign $catalogDesign
* @param MagentoCatalogModelSession $catalogSession
* @param MagentoFrameworkRegistry $coreRegistry
* @param MagentoStoreModelStoreManagerInterface $storeManager
* @param MagentoCatalogUrlRewriteModelCategoryUrlPathGenerator $categoryUrlPathGenerator
* @param MagentoFrameworkViewResultPageFactory $resultPageFactory
* @param MagentoFrameworkControllerResultForwardFactory $resultForwardFactory
* @param Resolver $layerResolver
* @param CategoryRepositoryInterface $categoryRepository
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoCatalogModelDesign $catalogDesign,
MagentoCatalogModelSession $catalogSession,
MagentoFrameworkRegistry $coreRegistry,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoCatalogUrlRewriteModelCategoryUrlPathGenerator $categoryUrlPathGenerator,
PageFactory $resultPageFactory,
MagentoFrameworkControllerResultForwardFactory $resultForwardFactory,
Resolver $layerResolver,
CategoryRepositoryInterface $categoryRepository
)
parent::__construct($context);
$this->_storeManager = $storeManager;
$this->_catalogDesign = $catalogDesign;
$this->_catalogSession = $catalogSession;
$this->_coreRegistry = $coreRegistry;
$this->categoryUrlPathGenerator = $categoryUrlPathGenerator;
$this->resultPageFactory = $resultPageFactory;
$this->resultForwardFactory = $resultForwardFactory;
$this->layerResolver = $layerResolver;
$this->categoryRepository = $categoryRepository;
/**
* Initialize requested category object
*
* @return MagentoCatalogModelCategory
*/
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('MagentoCatalogHelperCategory')->canShow($category))
return false;
$this->_catalogSession->setLastVisitedCategoryId($category->getId());
$this->_coreRegistry->register('current_category', $category);
try
$this->_eventManager->dispatch(
'catalog_controller_category_init_after',
['category' => $category, 'controller_action' => $this]
);
catch (MagentoFrameworkExceptionLocalizedException $e)
$this->_objectManager->get('PsrLogLoggerInterface')->critical($e);
return false;
return $category;
/**
* Category view action
*
* @return MagentoFrameworkControllerResultInterface
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function execute()
if ($this->_request->getParam(MagentoFrameworkAppActionInterface::PARAM_NAME_URL_ENCODED))
return $this->resultRedirectFactory->create()->setUrl($this->_redirect->getRedirectUrl());
$category = $this->_initCategory();
if ($category)
$this->layerResolver->create(Resolver::CATALOG_LAYER_CATEGORY);
$settings = $this->_catalogDesign->getDesignSettings($category);
// apply custom design
if ($settings->getCustomDesign())
$this->_catalogDesign->applyCustomDesign($settings->getCustomDesign());
$this->_catalogSession->setLastViewedCategoryId($category->getId());
$page = $this->resultPageFactory->create();
// apply custom layout (page) template once the blocks are generated
if ($settings->getPageLayout())
$page->getConfig()->setPageLayout($settings->getPageLayout());
$hasChildren = $category->hasChildren();
if ($category->getIsAnchor())
$type = $hasChildren ? 'layered' : 'layered_without_children';
else
$type = $hasChildren ? 'default' : 'default_without_children';
if (!$hasChildren)
// Two levels removed from parent. Need to add default page type.
$parentType = strtok($type, '_');
$page->addPageLayoutHandles(['type' => $parentType]);
$page->addPageLayoutHandles(['type' => $type, 'id' => $category->getId()]);
// apply custom layout update once layout is loaded
$layoutUpdates = $settings->getLayoutUpdates();
if ($layoutUpdates && is_array($layoutUpdates))
foreach ($layoutUpdates as $layoutUpdate)
$page->addUpdate($layoutUpdate);
$page->addPageLayoutHandles(['layout_update' => md5($layoutUpdate)]);
$page->getConfig()->addBodyClass('page-products')
->addBodyClass('categorypath-' . $this->categoryUrlPathGenerator->getUrlPath($category))
->addBodyClass('category-' . $category->getUrlKey());
$block = $page->getLayout()->getBlock('category.products');
$this->getResponse()->setBody($block->toHtml());
category magento-2.1 layered-navigation category-products category-view
I have customized a Layered navigation modules in Magento 2. However, I can't write a controller to get all content of category page and load into controller page(localhost/magento2/layerednavigation/index/index/id/17?cat=15&style_general=117
).
The controller page will load but it don't filler product. I have checked and seen that controller page don't get param(cat, style_general....).
Summary, I want write controller to load category page. However, I am facing the issue is the filler don't work into controller page.
Please refer code:
1. Code for layout: layerednavigation_index_index.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2016 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>
<referenceContainer name="columns.top">
<container name="category.view.container" htmlTag="div" htmlClass="category-view" after="-">
<block class="MagentoCatalogBlockCategoryView" name="category.image" template="Magento_Catalog::category/image.phtml"/>
<block class="MagentoCatalogBlockCategoryView" name="category.description" template="Magento_Catalog::category/description.phtml"/>
<block class="MagentoCatalogBlockCategoryView" name="category.cms" template="Magento_Catalog::category/cms.phtml"/>
</container>
</referenceContainer>
<referenceContainer name="content">
<block class="MagentoCatalogBlockCategoryView" name="category.products" template="Magento_Catalog::category/products.phtml">
<block class="MagentoCatalogBlockProductListProduct" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">
<container name="category.product.list.additional" as="additional" />
<block class="MagentoFrameworkViewElementRendererList" name="category.product.type.details.renderers" as="details.renderers">
<block class="MagentoFrameworkViewElementTemplate" as="default"/>
</block>
<block class="MagentoCatalogBlockProductProductListToolbar" name="product_list_toolbar" template="Magento_Catalog::product/list/toolbar.phtml">
<block class="MagentoThemeBlockHtmlPager" name="product_list_toolbar_pager"/>
<!-- The following code shows how to set your own pager increments -->
<!--
<action method="setDefaultListPerPage">
<argument name="limit" xsi:type="string">4</argument>
</action>
<action method="setDefaultGridPerPage">
<argument name="limit" xsi:type="string">3</argument>
</action>
<action method="addPagerLimit">
<argument name="mode" xsi:type="string">list</argument>
<argument name="limit" xsi:type="string">2</argument>
</action>
<action method="addPagerLimit">
<argument name="mode" xsi:type="string">list</argument>
<argument name="limit" xsi:type="string">4</argument>
</action>
<action method="addPagerLimit">
<argument name="mode" xsi:type="string">list</argument>
<argument name="limit" xsi:type="string">6</argument>
</action>
<action method="addPagerLimit">
<argument name="mode" xsi:type="string">list</argument>
<argument name="limit" xsi:type="string">8</argument>
</action>
<action method="addPagerLimit" translate="label">
<argument name="mode" xsi:type="string">list</argument>
<argument name="limit" xsi:type="string">all</argument>
<argument name="label" xsi:type="string">All</argument>
</action>
<action method="addPagerLimit">
<argument name="mode" xsi:type="string">grid</argument>
<argument name="limit" xsi:type="string">3</argument>
</action>
<action method="addPagerLimit">
<argument name="mode" xsi:type="string">grid</argument>
<argument name="limit" xsi:type="string">6</argument>
</action>
<action method="addPagerLimit">
<argument name="mode" xsi:type="string">grid</argument>
<argument name="limit" xsi:type="string">9</argument>
</action>
<action method="addPagerLimit" translate="label">
<argument name="mode" xsi:type="string">grid</argument>
<argument name="limit" xsi:type="string">all</argument>
<argument name="label" xsi:type="string">All</argument>
</action>
-->
</block>
<action method="setToolbarBlockName">
<argument name="name" xsi:type="string">product_list_toolbar</argument>
</action>
</block>
</block>
<block class="MagentoCookieBlockRequireCookie" name="require-cookie" template="Magento_Cookie::require_cookie.phtml">
<arguments>
<argument name="triggers" xsi:type="array">
<item name="compareProductLink" xsi:type="string">.action.tocompare</item>
</argument>
</arguments>
</block>
</referenceContainer>
<referenceBlock name="page.main.title">
<arguments>
<argument name="id" xsi:type="string">page-title-heading</argument>
<argument name="add_base_attribute_aria" xsi:type="string">page-title-heading toolbar-amount</argument>
</arguments>
<block class="MagentoCatalogBlockCategoryRssLink" name="rss.link" template="Magento_Catalog::category/rss.phtml"/>
</referenceBlock>
</body>
</page>
Controller code: index.php
<?php
namespace EmthemesLayeredNavigationProControllerIndex;
use MagentoCatalogApiCategoryRepositoryInterface;
use MagentoCatalogModelLayerResolver;
use MagentoFrameworkExceptionNoSuchEntityException;
use MagentoFrameworkViewResultPageFactory;
class Index extends MagentoFrameworkAppActionAction
protected $_coreRegistry = null;
protected $_catalogSession;
protected $_catalogDesign;
protected $_storeManager;
protected $categoryUrlPathGenerator;
protected $resultPageFactory;
protected $resultForwardFactory;
private $layerResolver;
protected $categoryRepository;
/**
* Constructor
*
* @param MagentoFrameworkAppActionContext $context
* @param MagentoCatalogModelDesign $catalogDesign
* @param MagentoCatalogModelSession $catalogSession
* @param MagentoFrameworkRegistry $coreRegistry
* @param MagentoStoreModelStoreManagerInterface $storeManager
* @param MagentoCatalogUrlRewriteModelCategoryUrlPathGenerator $categoryUrlPathGenerator
* @param MagentoFrameworkViewResultPageFactory $resultPageFactory
* @param MagentoFrameworkControllerResultForwardFactory $resultForwardFactory
* @param Resolver $layerResolver
* @param CategoryRepositoryInterface $categoryRepository
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoCatalogModelDesign $catalogDesign,
MagentoCatalogModelSession $catalogSession,
MagentoFrameworkRegistry $coreRegistry,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoCatalogUrlRewriteModelCategoryUrlPathGenerator $categoryUrlPathGenerator,
PageFactory $resultPageFactory,
MagentoFrameworkControllerResultForwardFactory $resultForwardFactory,
Resolver $layerResolver,
CategoryRepositoryInterface $categoryRepository
)
parent::__construct($context);
$this->_storeManager = $storeManager;
$this->_catalogDesign = $catalogDesign;
$this->_catalogSession = $catalogSession;
$this->_coreRegistry = $coreRegistry;
$this->categoryUrlPathGenerator = $categoryUrlPathGenerator;
$this->resultPageFactory = $resultPageFactory;
$this->resultForwardFactory = $resultForwardFactory;
$this->layerResolver = $layerResolver;
$this->categoryRepository = $categoryRepository;
/**
* Initialize requested category object
*
* @return MagentoCatalogModelCategory
*/
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('MagentoCatalogHelperCategory')->canShow($category))
return false;
$this->_catalogSession->setLastVisitedCategoryId($category->getId());
$this->_coreRegistry->register('current_category', $category);
try
$this->_eventManager->dispatch(
'catalog_controller_category_init_after',
['category' => $category, 'controller_action' => $this]
);
catch (MagentoFrameworkExceptionLocalizedException $e)
$this->_objectManager->get('PsrLogLoggerInterface')->critical($e);
return false;
return $category;
/**
* Category view action
*
* @return MagentoFrameworkControllerResultInterface
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function execute()
if ($this->_request->getParam(MagentoFrameworkAppActionInterface::PARAM_NAME_URL_ENCODED))
return $this->resultRedirectFactory->create()->setUrl($this->_redirect->getRedirectUrl());
$category = $this->_initCategory();
if ($category)
$this->layerResolver->create(Resolver::CATALOG_LAYER_CATEGORY);
$settings = $this->_catalogDesign->getDesignSettings($category);
// apply custom design
if ($settings->getCustomDesign())
$this->_catalogDesign->applyCustomDesign($settings->getCustomDesign());
$this->_catalogSession->setLastViewedCategoryId($category->getId());
$page = $this->resultPageFactory->create();
// apply custom layout (page) template once the blocks are generated
if ($settings->getPageLayout())
$page->getConfig()->setPageLayout($settings->getPageLayout());
$hasChildren = $category->hasChildren();
if ($category->getIsAnchor())
$type = $hasChildren ? 'layered' : 'layered_without_children';
else
$type = $hasChildren ? 'default' : 'default_without_children';
if (!$hasChildren)
// Two levels removed from parent. Need to add default page type.
$parentType = strtok($type, '_');
$page->addPageLayoutHandles(['type' => $parentType]);
$page->addPageLayoutHandles(['type' => $type, 'id' => $category->getId()]);
// apply custom layout update once layout is loaded
$layoutUpdates = $settings->getLayoutUpdates();
if ($layoutUpdates && is_array($layoutUpdates))
foreach ($layoutUpdates as $layoutUpdate)
$page->addUpdate($layoutUpdate);
$page->addPageLayoutHandles(['layout_update' => md5($layoutUpdate)]);
$page->getConfig()->addBodyClass('page-products')
->addBodyClass('categorypath-' . $this->categoryUrlPathGenerator->getUrlPath($category))
->addBodyClass('category-' . $category->getUrlKey());
$block = $page->getLayout()->getBlock('category.products');
$this->getResponse()->setBody($block->toHtml());
category magento-2.1 layered-navigation category-products category-view
category magento-2.1 layered-navigation category-products category-view
edited Nov 3 '16 at 5:26
Ashish Jagnani
4,64922057
4,64922057
asked Nov 3 '16 at 3:12
Minh Tam PhamMinh Tam Pham
9037
9037
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
All you need to do is get category id from url and set in registery and use it in block,
This is what controller execute contains:
$catId = (int)$this->getRequest()->getParam('id', false);
$this->_coreRegistry->register('catid', $catId);
Here is the block function where you can access your registery category id and using it just fetch all categories :
Initialization:
protected $_coreRegistry;
private $_itemCollectionFactory;
Constructor :
public function __construct(
...
MagentoCatalogModelResourceModelCategoryCollectionFactory $categoriesCollection,
MagentoFrameworkRegistry $coreRegistry,
...
)
...
$this->_itemCollectionFactory = $categoriesCollection;
$this->_coreRegistry = $coreRegistry;
...
And here is the function to get category list:
public function getCategoriesList()
$cid = $this->_coreRegistry->registry('catid');
$categoryFactory = $this->_itemCollectionFactory->create()
->addAttributeToSelect('*');
return $categoryFactory;
i have yet to filter into controller page. Could you show the instruction for me? Thanks for your help. I am a new Magento.
– Minh Tam Pham
Nov 3 '16 at 7:58
you can fetch category id from url in controller execute method, have you tried?
– Ronak Chauhan
Nov 3 '16 at 8:50
I have tried. However, it doesn't have respond. Could you help me, please?
– Minh Tam Pham
Nov 3 '16 at 9:00
sure, simply copy this line:$catId = (int)$this->getRequest()->getParam('cat', false);
and print $catId and check what you are getting. you need to put this in controller execute method.
– Ronak Chauhan
Nov 3 '16 at 9:01
I have performed like you have guided. It don't effect to filter into controller page.
– Minh Tam Pham
Nov 3 '16 at 11:08
|
show 1 more comment
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f143854%2fmagento-2-how-to-write-controller-to-load-category-page%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
All you need to do is get category id from url and set in registery and use it in block,
This is what controller execute contains:
$catId = (int)$this->getRequest()->getParam('id', false);
$this->_coreRegistry->register('catid', $catId);
Here is the block function where you can access your registery category id and using it just fetch all categories :
Initialization:
protected $_coreRegistry;
private $_itemCollectionFactory;
Constructor :
public function __construct(
...
MagentoCatalogModelResourceModelCategoryCollectionFactory $categoriesCollection,
MagentoFrameworkRegistry $coreRegistry,
...
)
...
$this->_itemCollectionFactory = $categoriesCollection;
$this->_coreRegistry = $coreRegistry;
...
And here is the function to get category list:
public function getCategoriesList()
$cid = $this->_coreRegistry->registry('catid');
$categoryFactory = $this->_itemCollectionFactory->create()
->addAttributeToSelect('*');
return $categoryFactory;
i have yet to filter into controller page. Could you show the instruction for me? Thanks for your help. I am a new Magento.
– Minh Tam Pham
Nov 3 '16 at 7:58
you can fetch category id from url in controller execute method, have you tried?
– Ronak Chauhan
Nov 3 '16 at 8:50
I have tried. However, it doesn't have respond. Could you help me, please?
– Minh Tam Pham
Nov 3 '16 at 9:00
sure, simply copy this line:$catId = (int)$this->getRequest()->getParam('cat', false);
and print $catId and check what you are getting. you need to put this in controller execute method.
– Ronak Chauhan
Nov 3 '16 at 9:01
I have performed like you have guided. It don't effect to filter into controller page.
– Minh Tam Pham
Nov 3 '16 at 11:08
|
show 1 more comment
All you need to do is get category id from url and set in registery and use it in block,
This is what controller execute contains:
$catId = (int)$this->getRequest()->getParam('id', false);
$this->_coreRegistry->register('catid', $catId);
Here is the block function where you can access your registery category id and using it just fetch all categories :
Initialization:
protected $_coreRegistry;
private $_itemCollectionFactory;
Constructor :
public function __construct(
...
MagentoCatalogModelResourceModelCategoryCollectionFactory $categoriesCollection,
MagentoFrameworkRegistry $coreRegistry,
...
)
...
$this->_itemCollectionFactory = $categoriesCollection;
$this->_coreRegistry = $coreRegistry;
...
And here is the function to get category list:
public function getCategoriesList()
$cid = $this->_coreRegistry->registry('catid');
$categoryFactory = $this->_itemCollectionFactory->create()
->addAttributeToSelect('*');
return $categoryFactory;
i have yet to filter into controller page. Could you show the instruction for me? Thanks for your help. I am a new Magento.
– Minh Tam Pham
Nov 3 '16 at 7:58
you can fetch category id from url in controller execute method, have you tried?
– Ronak Chauhan
Nov 3 '16 at 8:50
I have tried. However, it doesn't have respond. Could you help me, please?
– Minh Tam Pham
Nov 3 '16 at 9:00
sure, simply copy this line:$catId = (int)$this->getRequest()->getParam('cat', false);
and print $catId and check what you are getting. you need to put this in controller execute method.
– Ronak Chauhan
Nov 3 '16 at 9:01
I have performed like you have guided. It don't effect to filter into controller page.
– Minh Tam Pham
Nov 3 '16 at 11:08
|
show 1 more comment
All you need to do is get category id from url and set in registery and use it in block,
This is what controller execute contains:
$catId = (int)$this->getRequest()->getParam('id', false);
$this->_coreRegistry->register('catid', $catId);
Here is the block function where you can access your registery category id and using it just fetch all categories :
Initialization:
protected $_coreRegistry;
private $_itemCollectionFactory;
Constructor :
public function __construct(
...
MagentoCatalogModelResourceModelCategoryCollectionFactory $categoriesCollection,
MagentoFrameworkRegistry $coreRegistry,
...
)
...
$this->_itemCollectionFactory = $categoriesCollection;
$this->_coreRegistry = $coreRegistry;
...
And here is the function to get category list:
public function getCategoriesList()
$cid = $this->_coreRegistry->registry('catid');
$categoryFactory = $this->_itemCollectionFactory->create()
->addAttributeToSelect('*');
return $categoryFactory;
All you need to do is get category id from url and set in registery and use it in block,
This is what controller execute contains:
$catId = (int)$this->getRequest()->getParam('id', false);
$this->_coreRegistry->register('catid', $catId);
Here is the block function where you can access your registery category id and using it just fetch all categories :
Initialization:
protected $_coreRegistry;
private $_itemCollectionFactory;
Constructor :
public function __construct(
...
MagentoCatalogModelResourceModelCategoryCollectionFactory $categoriesCollection,
MagentoFrameworkRegistry $coreRegistry,
...
)
...
$this->_itemCollectionFactory = $categoriesCollection;
$this->_coreRegistry = $coreRegistry;
...
And here is the function to get category list:
public function getCategoriesList()
$cid = $this->_coreRegistry->registry('catid');
$categoryFactory = $this->_itemCollectionFactory->create()
->addAttributeToSelect('*');
return $categoryFactory;
answered Nov 3 '16 at 5:17
Ronak ChauhanRonak Chauhan
4,26911751
4,26911751
i have yet to filter into controller page. Could you show the instruction for me? Thanks for your help. I am a new Magento.
– Minh Tam Pham
Nov 3 '16 at 7:58
you can fetch category id from url in controller execute method, have you tried?
– Ronak Chauhan
Nov 3 '16 at 8:50
I have tried. However, it doesn't have respond. Could you help me, please?
– Minh Tam Pham
Nov 3 '16 at 9:00
sure, simply copy this line:$catId = (int)$this->getRequest()->getParam('cat', false);
and print $catId and check what you are getting. you need to put this in controller execute method.
– Ronak Chauhan
Nov 3 '16 at 9:01
I have performed like you have guided. It don't effect to filter into controller page.
– Minh Tam Pham
Nov 3 '16 at 11:08
|
show 1 more comment
i have yet to filter into controller page. Could you show the instruction for me? Thanks for your help. I am a new Magento.
– Minh Tam Pham
Nov 3 '16 at 7:58
you can fetch category id from url in controller execute method, have you tried?
– Ronak Chauhan
Nov 3 '16 at 8:50
I have tried. However, it doesn't have respond. Could you help me, please?
– Minh Tam Pham
Nov 3 '16 at 9:00
sure, simply copy this line:$catId = (int)$this->getRequest()->getParam('cat', false);
and print $catId and check what you are getting. you need to put this in controller execute method.
– Ronak Chauhan
Nov 3 '16 at 9:01
I have performed like you have guided. It don't effect to filter into controller page.
– Minh Tam Pham
Nov 3 '16 at 11:08
i have yet to filter into controller page. Could you show the instruction for me? Thanks for your help. I am a new Magento.
– Minh Tam Pham
Nov 3 '16 at 7:58
i have yet to filter into controller page. Could you show the instruction for me? Thanks for your help. I am a new Magento.
– Minh Tam Pham
Nov 3 '16 at 7:58
you can fetch category id from url in controller execute method, have you tried?
– Ronak Chauhan
Nov 3 '16 at 8:50
you can fetch category id from url in controller execute method, have you tried?
– Ronak Chauhan
Nov 3 '16 at 8:50
I have tried. However, it doesn't have respond. Could you help me, please?
– Minh Tam Pham
Nov 3 '16 at 9:00
I have tried. However, it doesn't have respond. Could you help me, please?
– Minh Tam Pham
Nov 3 '16 at 9:00
sure, simply copy this line:
$catId = (int)$this->getRequest()->getParam('cat', false);
and print $catId and check what you are getting. you need to put this in controller execute method.– Ronak Chauhan
Nov 3 '16 at 9:01
sure, simply copy this line:
$catId = (int)$this->getRequest()->getParam('cat', false);
and print $catId and check what you are getting. you need to put this in controller execute method.– Ronak Chauhan
Nov 3 '16 at 9:01
I have performed like you have guided. It don't effect to filter into controller page.
– Minh Tam Pham
Nov 3 '16 at 11:08
I have performed like you have guided. It don't effect to filter into controller page.
– Minh Tam Pham
Nov 3 '16 at 11:08
|
show 1 more comment
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f143854%2fmagento-2-how-to-write-controller-to-load-category-page%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown