Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Attributes
SwitchAction Class Reference
Inheritance diagram for SwitchAction:
Action AbstractAction ActionInterface

Public Member Functions

 __construct (ActionContext $context, StoreCookieManagerInterface $storeCookieManager, HttpContext $httpContext, StoreRepositoryInterface $storeRepository, StoreManagerInterface $storeManager, StoreSwitcherInterface $storeSwitcher=null)
 
 execute ()
 
- Public Member Functions inherited from Action
 __construct (Context $context)
 
 dispatch (RequestInterface $request)
 
 getActionFlag ()
 
- Public Member Functions inherited from AbstractAction
 __construct (Context $context)
 
 dispatch (RequestInterface $request)
 
 getRequest ()
 
 getResponse ()
 

Protected Attributes

 $storeCookieManager
 
 $httpContext
 
 $storeRepository
 
 $storeManager
 
- Protected Attributes inherited from Action
 $_objectManager
 
 $_sessionNamespace
 
 $_eventManager
 
 $_actionFlag
 
 $_redirect
 
 $_view
 
 $_url
 
 $messageManager
 
- Protected Attributes inherited from AbstractAction
 $_request
 
 $_response
 
 $resultRedirectFactory
 
 $resultFactory
 

Additional Inherited Members

- Data Fields inherited from ActionInterface
const FLAG_NO_DISPATCH = 'no-dispatch'
 
const FLAG_NO_POST_DISPATCH = 'no-postDispatch'
 
const FLAG_NO_DISPATCH_BLOCK_EVENT = 'no-beforeGenerateLayoutBlocksDispatch'
 
const PARAM_NAME_BASE64_URL = 'r64'
 
const PARAM_NAME_URL_ENCODED = 'uenc'
 
- Protected Member Functions inherited from Action
 _forward ($action, $controller=null, $module=null, array $params=null)
 
 _redirect ($path, $arguments=[])
 

Detailed Description

Handles store switching url and makes redirect.

@SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 27 of file SwitchAction.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( ActionContext  $context,
StoreCookieManagerInterface  $storeCookieManager,
HttpContext  $httpContext,
StoreRepositoryInterface  $storeRepository,
StoreManagerInterface  $storeManager,
StoreSwitcherInterface  $storeSwitcher = null 
)

Initialize dependencies.

Parameters
ActionContext$context
StoreCookieManagerInterface$storeCookieManager
HttpContext$httpContext
StoreRepositoryInterface$storeRepository
StoreManagerInterface$storeManager
StoreSwitcherInterface$storeSwitcher

Definition at line 66 of file SwitchAction.php.

73  {
74  parent::__construct($context);
75  $this->storeCookieManager = $storeCookieManager;
76  $this->httpContext = $httpContext;
77  $this->storeRepository = $storeRepository;
78  $this->storeManager = $storeManager;
79  $this->messageManager = $context->getMessageManager();
80  $this->storeSwitcher = $storeSwitcher ?: ObjectManager::getInstance()->get(StoreSwitcherInterface::class);
81  }

Member Function Documentation

◆ execute()

execute ( )

Execute action

Returns
void
Exceptions
StoreSwitcher

Implements ActionInterface.

Definition at line 89 of file SwitchAction.php.

90  {
91  $targetStoreCode = $this->_request->getParam(
93  $this->storeCookieManager->getStoreCodeFromCookie()
94  );
95  $fromStoreCode = $this->_request->getParam('___from_store');
96 
97  $requestedUrlToRedirect = $this->_redirect->getRedirectUrl();
98  $redirectUrl = $requestedUrlToRedirect;
99 
100  $error = null;
101  try {
102  $fromStore = $this->storeRepository->get($fromStoreCode);
103  $targetStore = $this->storeRepository->getActiveStoreByCode($targetStoreCode);
104  } catch (StoreIsInactiveException $e) {
105  $error = __('Requested store is inactive');
106  } catch (NoSuchEntityException $e) {
107  $error = __("The store that was requested wasn't found. Verify the store and try again.");
108  }
109  if ($error !== null) {
110  $this->messageManager->addErrorMessage($error);
111  } else {
112  $redirectUrl = $this->storeSwitcher->switch($fromStore, $targetStore, $requestedUrlToRedirect);
113  }
114 
115  $this->getResponse()->setRedirect($redirectUrl);
116  }
_redirect($path, $arguments=[])
Definition: Action.php:167
__()
Definition: __.php:13

Field Documentation

◆ $httpContext

$httpContext
protected

Definition at line 38 of file SwitchAction.php.

◆ $storeCookieManager

$storeCookieManager
protected

Definition at line 32 of file SwitchAction.php.

◆ $storeManager

$storeManager
protected

Definition at line 49 of file SwitchAction.php.

◆ $storeRepository

$storeRepository
protected

Definition at line 43 of file SwitchAction.php.


The documentation for this class was generated from the following file: