2

I am creating a controller extending \Magento\Customer\Controller\AbstractAccount but it automatically get redirected to http://example.com/customer/account/login/ how can I stop that. basically all the controller under Magento_customer/Controller/Account are extended by \Magento\Customer\Controller\AbstractAccount

but some are redirecting same as mine,like

Magento_customer/Controller/Account/index.php

while some are not redirected, like

Magento_customer/Controller/Account/create.php

Controller code

<?php
namespace Vendor\Module\Controller\Account;

class ControllerName extends \Magento\Customer\Controller\AbstractAccount
{
    public function execute()
    {
        echo "Test";
    }
}
  • Did you get this to work? – drew7721 Feb 20 '18 at 5:49
  • Can you Please Mark any answer as accepted from below which you found helpful or post your solution and mark it as accepted in order to close the question. – Piyush Mar 7 '18 at 8:24
  • @Piyush I haven't tried it yet planing to implement this by next week, accordingly accept answer – Arun Karnawat Mar 7 '18 at 9:44
2

Below method is responsible for redirection of any customer action

File path:- /vendor/magento/module-customer/Controller/Plugin/Account.php

public function beforeDispatch(AbstractAction $subject, RequestInterface $request)
{
    $action = strtolower($request->getActionName());
    $pattern = '/^(' . implode('|', $this->allowedActions) . ')$/i';

    if (!preg_match($pattern, $action)) {
        if (!$this->session->authenticate()) {
            $subject->getActionFlag()->set('', ActionInterface::FLAG_NO_DISPATCH, true);
        }
    } else {
        $this->session->setNoReferer(true);
    }
}

This method will redirect your action to customer/account/login if it is not there in $this->allowedActions array.

To solve your problem you need to override this method and add an extra condition in if statement for proper redirection of your action.

You can add condition for you controller like below

public function beforeDispatch(AbstractAction $subject, RequestInterface $request)
{
    $router = strtolower($request->getFullActionName());
    $action = strtolower($request->getActionName());
    $pattern = '/^(' . implode('|', $this->allowedActions) . ')$/i';

    if ($router != 'yourRouter_controller_action' && !preg_match($pattern, $action)) {
        if (!$this->session->authenticate()) {
            $subject->getActionFlag()->set('', ActionInterface::FLAG_NO_DISPATCH, true);
        }
    } else {
        $this->session->setNoReferer(true);
    }
}
  • How can I prevent my controller from redirecting. – Arun Karnawat Feb 12 '18 at 6:33
1

Simple way:

extend the Magento/Customer/Controller/Plugin/Account class in your module.

rewrite the constructor and pass an array[ containing your controller name ], as the 2nd argument of the parent constructor. (parent::__constru....)

In your di.xml file set a preference to use your plugin instead of the one in Customer.

See the file by file below, might make more sense than the above :)

Controller :

<?php
namespace Foo\Bar\Controller\Account;

use Magento\Customer\Controller\AbstractAccount;
use Magento\Framework\App\ResponseInterface;

class Something extends AbstractAccount
{

public function execute()
{
    echo "Test";
}
}

Plugin :

    <?php
    namespace Foo\Bar\Plugin;

    class Account extends \Magento\Customer\Controller\Plugin\Account
    {
        public function __construct(
                  \Magento\Customer\Model\Session $customerSession,
                  array $allowedActions = []
     ) {
        parent::__construct(
            $customerSession,
            array_merge(
                $allowedActions,
                [
                    'something' => 'something'
                ]
            )
        );
      }
    }

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">
      <preference for="Magento\Customer\Controller\Plugin\Account" type="Foo\Bar\Plugin\Account"/>
 </config>
  • Obviously, change the namespace to match your own... – drew7721 Feb 13 '18 at 23:09
  • just tested it.. works. And I think it's the simplest way. I see the other answer but why mess with code if you don't need to ;) cheers – drew7721 Feb 13 '18 at 23:22
  • I too have thought of this solution but I didn't opted it because it will work for every something action regardless of module, the much simpler solution will be to change action name to any one that already exist in the allowed array. – Piyush Feb 14 '18 at 5:06
  • 1
    Not sure any actions are pre-allowed, haven't checked. And in my opinion if another module wants to use this even better, no need to add a second action name. Security is not the issue here, it's extending the AbstractController without forcing the login. What you do with it.. that's up to the developer. I think it's a problem that does not exist. I say the option is there, use it! don't reinvent the wheel just cause you can. Plus, there are other security checks in on the account side that would prevent someone form accessing secure information trough this controller, check them out.. ;) – drew7721 Feb 14 '18 at 5:23
  • and Thx for the edits, btw! ;) cheers – drew7721 Feb 14 '18 at 5:27

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.