Question

I have added custom link in my account section

view/frontend/layout/customer_account.xml

    <?xml version="1.0"?>
    <!--
    /**
     * Copyright © Magento, Inc. All rights reserved.
     * See COPYING.txt for license details.
     */
    -->
    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <body>
            <referenceBlock name="customer_account_navigation">
                <block class="Magento\Customer\Block\Account\SortLinkInterface" name="customer-prescription" after="-">
                    <arguments>
                        <argument name="path" xsi:type="string">prescription</argument>
                        <argument name="label" xsi:type="string" translate="true">Prescriptions</argument>
                        <argument name="sortOrder" xsi:type="number">5</argument>
                    </arguments>
                </block>
            </referenceBlock>
        </body>
    </page>

CONTROLLER

    use Magento\Framework\App\Action\Context;
    use Magento\Framework\View\Result\PageFactory;
    use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;

    class Index extends \Magento\Framework\App\Action\Action implements HttpGetActionInterface
    {
        /**
         * @var PageFactory
         */
        protected $resultPageFactory;

        /**
         * @param Context $context
         * @param PageFactory $resultPageFactory
         */
        public function __construct(
            Context $context,
            PageFactory $resultPageFactory
        ) {
            $this->resultPageFactory = $resultPageFactory;
            parent::__construct($context);
        }

        public function execute()
        {
            /** @var \Magento\Framework\View\Result\Page $resultPage */
            $resultPage = $this->resultPageFactory->create();
            $resultPage->getConfig()->getTitle()->set(__('Prescriptions'));

            $block = $resultPage->getLayout()->getBlock('customer.account.link.back');
            if ($block) {
                $block->setRefererUrl($this->_redirect->getRefererUrl());
            }
            return $resultPage;
        }
    }

it's working fine for logged in customer, but when session timtout means customer is not logged in is not redirecting to customer login page. It's still redirecting on prescription page. Can anyone help please

Was it helpful?

Solution

you need to add the code in di.xml

<type name="Magento\Sales\Controller\Order\History">
        <plugin name="authentication" type="Magento\Sales\Controller\Order\Plugin\Authentication"/>
    </type>

you need to put the controller path instead of Magento\Sales\Controller\Order\History

OTHER TIPS

In your controller change extends class to Magento\Customer\Controller\AbstractAccount

Your controller should be -

class YourClass extends \Magento\Customer\Controller\AbstractAccount
{
//Your Actions code
}

For Sales order history controller it is redirected by a plugin, you can find below code in di.xml of Magento_Sales module

<type name="Magento\Sales\Controller\Order\History">
        <plugin name="authentication" type="Magento\Sales\Controller\Order\Plugin\Authentication"/>
    </type>

Hi did you put checking in your controller Customer customer session. You can try below code in your controller.

I assume your module name is "Company_MyModle" and controler class name is Index.php

Controller : Index.php

<?php 
namespace Company\MyModle\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class Index extends \Magento\Framework\App\Action\Action
{
    /**
     * @var PageFactory
     */
    protected $resultPageFactory;

    protected $customerSession;

    /**
     * @param Context $context
     * @param PageFactory $resultPageFactory
     */
    public function __construct(
        Context $context,    
        PageFactory $resultPageFactory,
        \Magento\Customer\Model\Session $customerSession
    ) {
        parent::__construct($context);        
        $this->resultPageFactory = $resultPageFactory;
        $this->customerSession = $customerSession;
    }

    /**
     *
     * @return \Magento\Framework\View\Result\Page
     */
    public function execute()
    {

        if (!$this->customerSession->isLoggedIn()) {
            /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
            $resultRedirect = $this->resultRedirectFactory->create();
            $resultRedirect->setPath('customer/account/login');
            return $resultRedirect;
        }       
        $resultPage = $this->resultPageFactory->create();
        return $resultPage;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top