1

How can I create a custom error pages in my Magento 2 site. For example this pages: 500, 401, 403, and 404.

I tried for create block:

 $layout = $this->_view->getLayout();
 $block = $layout->createBlock('Training\Test\Block\Test');
 $this->getResponse()->appendBody($block->toHtml());

But not working return error

Catalog Layer has been already created

Below is my Controller

    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
    ) {
        $this->toolbar = $toolbar;
        $this->resultPageFactory = $resultPageFactory;
        $this->resultForwardFactory = $resultForwardFactory;
        $this->url = $url;
    }

    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)) 
        {
            /*$norouteUrl = $this->url->getUrl('noroute');
            $category->getResponse()->setRedirect($norouteUrl);
            return;*/
            return $this->resultForwardFactory->create()->forward('noroute');
        }
    }
}

In that I want to replace return $this->resultForwardFactory->create()->forward('noroute'); with my custom error page URL

  • Can you add your all codes like xml, controller and block files? – Sukumar Gorai Mar 5 at 10:50
1

For 404 and 503 there are content pages available in Content > Pages so you can edit that page but 403 error page.

First You also have to add Fastly troubleshooting, if you are adding it till now.

https://devdocs.magento.com/guides/v2.2/cloud/cdn/trouble-fastly.html

To edit the WAF error page:

  1. Log in to the Magento Admin UI.
  2. Click Stores > Settings > Configuration > Advanced > System.
  3. In the right pane, expand Full Page Cache > Fastly Configuration > Custom Synthetic Pages.

enter image description here

  1. Click Edit WAF page.
  2. Complete the fields to update the HTML.

enter image description here

  • Status—Select the 403 Forbidden status.
  • MIME type—Type text/html.
  • Content—Edit the default HTML response to add custom CSS and update the title and messaging as needed.

    1. Click Upload at the top of the page to upload the customized HTML source to the Fastly server.
    2. Click Save Config at the top of the page to save the updated configuration file.
    3. Refresh the cache.

      • In the notification at the top of the page, click the Cache Management link.
      • On the Cache Management page, click Flush Magento Cache.

For Reference Magento Documentation: Customize error pages

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.