0

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

3
  • what is your controller code can you add that code in question Sep 3, 2019 at 6:03
  • @JigsParmar have added please check Sep 3, 2019 at 6:28
  • please check my answer Sep 3, 2019 at 6:31

3 Answers 3

2

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

8
  • I know this way but why we have to additionally check using customer session.? i have checked in sales/order/history there is no code regarding customer session Sep 3, 2019 at 6:32
  • in the file Magento_Sales/etc/di.xml plugin authentication code is there you can search word Magento\Sales\Controller\Order\History and check Sep 3, 2019 at 6:39
  • if you want to execyte same like sales/order/history you need to add code in di.xml like same they put Sep 3, 2019 at 6:40
  • Perfect !!!!!!!!!!! Thank you so much Sep 3, 2019 at 6:42
  • 1
    @JigsParmar ,as per as, the community, you can NOT ask the user give upvote and accept. It is totally user call.
    – Amit Bera
    Sep 4, 2019 at 7:16
2

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>
2
  • But in Sales/order/History it's not extended why we have to extend it? @Piyush Sep 3, 2019 at 6:31
  • it depends on module, for all account action it extends this controller, for sales order there may be another way, may be any plugin or observer
    – Piyush
    Sep 3, 2019 at 6:34
0

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;
    }
}
1
  • I know this way but why we have to additionally check using customer session.? i have checked in sales/order/history there is no code regarding customer session Sep 3, 2019 at 5:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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