All How to redirect in observer


on event controller_action_predispatch
public function execute(\Magento\Framework\Event\Observer $observer)
{
# check if user is logged in
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');

if(!$customerSession->isLoggedIn())
{
$request = $objectManager->get('Magento\Framework\App\Request\Http');
//get instance for URL interface
/** @var \Magento\Framework\UrlInterface $urlInterface */
$urlInterface = $objectManager->get('Magento\Framework\UrlInterface');
// URL to redirect to
$url = $urlInterface->getUrl('customer/account/login');

if(strpos($request->getPathInfo(), '/customer/account/') !== 0)
{
# redirect to /customer/account/login
$observer->getControllerAction()
->getResponse()
->setRedirect($url);
}
}
}

namespace [VendorName]\[ModuleName]\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;

class ObserverforDisabledFrontend implements ObserverInterface
{

/**
* @var \Magento\Framework\App\ActionFlag
*/
protected $_actionFlag;

/**
* @var \Magento\Framework\Message\ManagerInterface
*/
protected $messageManager;

/**
* @var \Magento\Framework\App\Response\RedirectInterface
*/
protected $redirect;

/**
* @var Magento\Backend\Helper\Data
*/
private $HelperBackend;

/**
* @param \Magento\Framework\App\ActionFlag $actionFlag
* @param \Magento\Framework\Message\ManagerInterface $messageManager
* @param \Magento\Framework\App\Response\RedirectInterface $redirect
*/
public function __construct(
\Magento\Framework\App\ActionFlag $actionFlag,
\Magento\Framework\Message\ManagerInterface $messageManager,
\Magento\Framework\App\Response\RedirectInterface $redirect,
\Magento\Backend\Helper\Data $HelperBackend
) {
$this->_actionFlag = $actionFlag;
$this->messageManager = $messageManager;
$this->redirect = $redirect;
$this->HelperBackend = $HelperBackend;
}

/**
*
* @param \Magento\Framework\Event\Observer $observer
* @return void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
/** @var \Magento\Framework\App\Action\Action $controller */
$controller = $observer->getControllerAction();
$this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
/*$this->redirect->redirect($controller->getResponse(),
'https://www.google.co.in/?gfe_rd=cr&ei=OHoGWLzeIqnT8geDpr3wDQ');*/

$this->redirect->redirect($controller->getResponse(),$this->HelperBackend->getHomePageUrl());

}

}

//perso
public function __construct(
\Magento\Framework\App\ActionFlag $actionFlag,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Zend\Validator\Uri $uri,
\Magento\Framework\App\ResponseFactory $responseFactory,
\Magento\Framework\App\Response\RedirectInterface $redirect
) {
$this->actionFlag = $actionFlag;
$this->scopeConfig = $scopeConfig;
$this->uri = $uri;
$this->responseFactory = $responseFactory;
$this->redirect = $redirect;
}

/**
* Handler for 'customer_login' event.
*
* @param Observer $observer
* @return void
*/
public function execute(Observer $observer)
{
$particular_page = $this->scopeConfig->getValue('page_login/oropa_customer_login/redirect_login_page', \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITES);

if ($particular_page == null) {
$particular_page = $this->scopeConfig->getValue('page_login/oropa_customer_login/redirect_login_page');
}

if ($this->uri->isValid($particular_page)) {
$this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
$resultRedirect = $this->responseFactory->create();
$resultRedirect->setRedirect($particular_page)->sendResponse('200');
$controller = $observer->getControllerAction();
$this->redirect->redirect($controller->getResponse(), $particular_page);
}
return $this;
}

https://stackoverflow.com/questions/36902212/magento2-redirect-from-observer
https://magento.stackexchange.com/questions/113251/redirect-controller-in-magento-2
https://magento.stackexchange.com/questions/163340/magento-2-redirect-from-observer-not-working-on-category-page
https://magento.stackexchange.com/questions/112993/magento2-redirection-from-observer

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *