1

I am trying to redirect to custom 403 error page.

I have created plugin for controller as follow:

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">
<type name="Magento\Catalog\Controller\Category\View">
    <plugin name="Tatva_Catalog::changeCustomLimitData" type="Tatva\Catalog\Plugin\Controller\Catalog\View" />
</type> 
</config>

Custom\Catalog\Plugin\Controller\Catalog\View.php

<?php

    namespace Custom\Catalog\Plugin\Controller\Category;

    use Magento\Framework\View\Result\PageFactory;


        class View
        {
            public function __construct(
                \Magento\Framework\App\Action\Context $context,
                \Magento\Catalog\Block\Product\ProductList\Toolbar $toolbar,
                PageFactory $resultPageFactory,
                \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory,
                \Magento\Framework\UrlInterface $url,
                \Magento\Framework\App\RequestInterface $requestInterface
            ) {
                $this->toolbar = $toolbar;
                $this->resultPageFactory = $resultPageFactory;
                $this->resultForwardFactory = $resultForwardFactory;
                $this->url = $url;
                $this->requestInterface = $requestInterface;
            }

        public function beforeExecute(\Magento\Catalog\Controller\Category\View $category)
        {

            $product_limit = $category->getRequest()->getParam('product_list_limit');
            $available_limits = $this->toolbar->getAvailableLimit();
            if(!in_array($product_limit, $available_limits) && !empty($product_limit)) 
            {
                return ?
            }
        }
    }

In above code what should I add instead of ? to return to custom 403 error page URL in Magento2.

  • Have you tried this one ? $this->_redirect('Your403pageUrl'); – aravind Mar 6 at 4:38
  • @aravind what do u mean by 403routename ? can u give me details regarding how to create 403routename ? – Parthavi Mar 6 at 5:42
  • Do u mean the 403 page whole url ? – Parthavi Mar 6 at 5:49
  • @PrathapGunasekaran If we want to remain on www.example.com/index but want to redirect to something/index then what should be done? – Parthavi Mar 6 at 5:59
  • $this->resultRedirectFactory->create()->setPath('index/yourUrl'); – aravind Mar 6 at 6:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.