Magento 2 : How to redirect from a observer without exit or die (as it's not allowed by magento standards)What is being passed to an observerMagento2: Re-direct to custom URL from observerExtension observer not firing (Another extension is extending the class's from where the dispatch exists?)Preference is not working in Magento 2Change the price in quote while adding product to cart: magento2Give a percentage off to everyone that has a specific email address. ie. employee@mywebsite.comHow to redirect to cart after login using an observer in Magento 2Magento 2: not able to get order details in observerHow to stop flow from observer in Magento2 without using die or exit?Can I use same URL key for both the Product and Category?
How do I iterate equal values with the standard library?
Should I warn my boss I might take sick leave?
Why do we need a bootloader separate from our application program in microcontrollers?
Did William Shakespeare hide things in his writings?
Can you take the Dodge action while prone?
Boss furious on bad appraisal
Are "confidant" and "confident" homophones?
Why is there paternal, for fatherly, fraternal, for brotherly, but no similar word for sons?
Why did moving the mouse cursor cause Windows 95 to run more quickly?
How did שְׁלֹמֹה (shlomo) become Solomon?
What happens if the limit of 4 billion files was exceeded in an ext4 partition?
What is it called when the tritone is added to a minor scale?
Why weren't Gemini capsules given names?
How did the IEC decide to create kibibytes?
What's the big deal about the Nazgûl losing their horses?
Was the 45.9°C temperature in France in June 2019 the highest ever recorded in France?
Way to see all encrypted fields in Salesforce?
Taking advantage when the HR forgets to communicate the rules
What's the difference between 反面 and 一方?
What is the shape of the upper boundary of water hitting a screen?
Do intermediate subdomains need to exist?
Do I need to be legally qualified to install a Hive smart thermostat?
Sleepy tired vs physically tired
Question about targeting a Hexproof creature
Magento 2 : How to redirect from a observer without exit or die (as it's not allowed by magento standards)
What is being passed to an observerMagento2: Re-direct to custom URL from observerExtension observer not firing (Another extension is extending the class's from where the dispatch exists?)Preference is not working in Magento 2Change the price in quote while adding product to cart: magento2Give a percentage off to everyone that has a specific email address. ie. employee@mywebsite.comHow to redirect to cart after login using an observer in Magento 2Magento 2: not able to get order details in observerHow to stop flow from observer in Magento2 without using die or exit?Can I use same URL key for both the Product and Category?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I know of a couple of ways of redirecting to a given URL in observer but all of them uses exit or die and they would not work without it. So, if someone knows of a way of doing it without using exit or die then please let me know.
magento2 event-observer redirect redirect-url
add a comment |
I know of a couple of ways of redirecting to a given URL in observer but all of them uses exit or die and they would not work without it. So, if someone knows of a way of doing it without using exit or die then please let me know.
magento2 event-observer redirect redirect-url
add a comment |
I know of a couple of ways of redirecting to a given URL in observer but all of them uses exit or die and they would not work without it. So, if someone knows of a way of doing it without using exit or die then please let me know.
magento2 event-observer redirect redirect-url
I know of a couple of ways of redirecting to a given URL in observer but all of them uses exit or die and they would not work without it. So, if someone knows of a way of doing it without using exit or die then please let me know.
magento2 event-observer redirect redirect-url
magento2 event-observer redirect redirect-url
edited Jun 26 at 13:37
Vivek Kumar
asked Apr 5 '17 at 6:04
Vivek KumarVivek Kumar
2,7632 gold badges7 silver badges32 bronze badges
2,7632 gold badges7 silver badges32 bronze badges
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
use setRedirect
<?php
namespace [Vendor][modulename]Observer;
use MagentoFrameworkEventObserver;
use MagentoFrameworkEventObserverInterface;
class [YourClass] implements ObserverInterface
protected $_responseFactory;
protected $_redirect;
protected $_url;
public function __construct(
......
MagentoFrameworkAppResponseFactory $responseFactory,
MagentoFrameworkUrlInterface $url,
MagentoFrameworkAppResponseHttp $redirect,
......
)
$this->_responseFactory = $responseFactory;
$this->_url = $url;
$this->_redirect = $redirect;
public function execute(Observer $observer)
$event = $observer->getEvent();
$CustomRedirectionUrl = $this->_url->getUrl('[ModuleName]/[ModuleName]/[[Action]');
$this->_redirect->setRedirect($CustomRedirectionUrl);
I am using above code for redirecting and its working when we exit; after sendResponse() but otherwise its not working. Also i should mention that my observer is observing layout_generate_blocks_after event .
– Vivek Kumar
Apr 5 '17 at 7:15
@Neo i update the code try it and feedback.
– Qaisar Satti
Apr 6 '17 at 9:29
The code you've updated works only for controllers and not for observers
– Vivek Kumar
Apr 6 '17 at 10:24
@Neo i am using this code in observer it is working for me. i am using this eventcontroller_action_predispatch
– Qaisar Satti
Apr 6 '17 at 10:29
1
I've checked properly now and the code is working for me . Thanks
– Vivek Kumar
Apr 6 '17 at 11:27
add a comment |
Without exit or die redirecting to a given URL in observer you need to stop the dispatch event using Action::FLAG_NO_DISPATCH
namespace [Vendor][modulename]Observer;
use MagentoFrameworkEventObserverInterface;
class [YourClass] implements ObserverInterface
protected $urlManager;
protected $actionFlag;
protected $redirect;
public function __construct(
......
MagentoFrameworkUrlInterface $url,
MagentoFrameworkAppActionFlag $actionFlag,
MagentoFrameworkAppResponseRedirectInterface $redirect,
......
)
$this->urlManager = $url;
$this->actionFlag = $actionFlag;
$this->redirect = $redirect;
public function execute(MagentoFrameworkEventObserver $observer)
$controller = $observer->getControllerAction();
// stop the dispatch event.
$this->actionFlag->set('', MagentoFrameworkAppActionAction::FLAG_NO_DISPATCH, true);
//YOUR-ACTION eq to ex. for create acount '*/*/create'
$defaultUrl = $this->urlManager->getUrl('YOUR-ACTION', ['_secure' => true]);
$controller->getResponse()->setRedirect($this->redirect->error($defaultUrl));
I used this code but I am getting following error : Uncaught Error: Call to a member function getResponse()
– David Coder
Dec 12 '18 at 6:06
I am using checkout_cart_product_add_after event
– David Coder
Dec 12 '18 at 6:07
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f167707%2fmagento-2-how-to-redirect-from-a-observer-without-exit-or-die-as-its-not-all%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
use setRedirect
<?php
namespace [Vendor][modulename]Observer;
use MagentoFrameworkEventObserver;
use MagentoFrameworkEventObserverInterface;
class [YourClass] implements ObserverInterface
protected $_responseFactory;
protected $_redirect;
protected $_url;
public function __construct(
......
MagentoFrameworkAppResponseFactory $responseFactory,
MagentoFrameworkUrlInterface $url,
MagentoFrameworkAppResponseHttp $redirect,
......
)
$this->_responseFactory = $responseFactory;
$this->_url = $url;
$this->_redirect = $redirect;
public function execute(Observer $observer)
$event = $observer->getEvent();
$CustomRedirectionUrl = $this->_url->getUrl('[ModuleName]/[ModuleName]/[[Action]');
$this->_redirect->setRedirect($CustomRedirectionUrl);
I am using above code for redirecting and its working when we exit; after sendResponse() but otherwise its not working. Also i should mention that my observer is observing layout_generate_blocks_after event .
– Vivek Kumar
Apr 5 '17 at 7:15
@Neo i update the code try it and feedback.
– Qaisar Satti
Apr 6 '17 at 9:29
The code you've updated works only for controllers and not for observers
– Vivek Kumar
Apr 6 '17 at 10:24
@Neo i am using this code in observer it is working for me. i am using this eventcontroller_action_predispatch
– Qaisar Satti
Apr 6 '17 at 10:29
1
I've checked properly now and the code is working for me . Thanks
– Vivek Kumar
Apr 6 '17 at 11:27
add a comment |
use setRedirect
<?php
namespace [Vendor][modulename]Observer;
use MagentoFrameworkEventObserver;
use MagentoFrameworkEventObserverInterface;
class [YourClass] implements ObserverInterface
protected $_responseFactory;
protected $_redirect;
protected $_url;
public function __construct(
......
MagentoFrameworkAppResponseFactory $responseFactory,
MagentoFrameworkUrlInterface $url,
MagentoFrameworkAppResponseHttp $redirect,
......
)
$this->_responseFactory = $responseFactory;
$this->_url = $url;
$this->_redirect = $redirect;
public function execute(Observer $observer)
$event = $observer->getEvent();
$CustomRedirectionUrl = $this->_url->getUrl('[ModuleName]/[ModuleName]/[[Action]');
$this->_redirect->setRedirect($CustomRedirectionUrl);
I am using above code for redirecting and its working when we exit; after sendResponse() but otherwise its not working. Also i should mention that my observer is observing layout_generate_blocks_after event .
– Vivek Kumar
Apr 5 '17 at 7:15
@Neo i update the code try it and feedback.
– Qaisar Satti
Apr 6 '17 at 9:29
The code you've updated works only for controllers and not for observers
– Vivek Kumar
Apr 6 '17 at 10:24
@Neo i am using this code in observer it is working for me. i am using this eventcontroller_action_predispatch
– Qaisar Satti
Apr 6 '17 at 10:29
1
I've checked properly now and the code is working for me . Thanks
– Vivek Kumar
Apr 6 '17 at 11:27
add a comment |
use setRedirect
<?php
namespace [Vendor][modulename]Observer;
use MagentoFrameworkEventObserver;
use MagentoFrameworkEventObserverInterface;
class [YourClass] implements ObserverInterface
protected $_responseFactory;
protected $_redirect;
protected $_url;
public function __construct(
......
MagentoFrameworkAppResponseFactory $responseFactory,
MagentoFrameworkUrlInterface $url,
MagentoFrameworkAppResponseHttp $redirect,
......
)
$this->_responseFactory = $responseFactory;
$this->_url = $url;
$this->_redirect = $redirect;
public function execute(Observer $observer)
$event = $observer->getEvent();
$CustomRedirectionUrl = $this->_url->getUrl('[ModuleName]/[ModuleName]/[[Action]');
$this->_redirect->setRedirect($CustomRedirectionUrl);
use setRedirect
<?php
namespace [Vendor][modulename]Observer;
use MagentoFrameworkEventObserver;
use MagentoFrameworkEventObserverInterface;
class [YourClass] implements ObserverInterface
protected $_responseFactory;
protected $_redirect;
protected $_url;
public function __construct(
......
MagentoFrameworkAppResponseFactory $responseFactory,
MagentoFrameworkUrlInterface $url,
MagentoFrameworkAppResponseHttp $redirect,
......
)
$this->_responseFactory = $responseFactory;
$this->_url = $url;
$this->_redirect = $redirect;
public function execute(Observer $observer)
$event = $observer->getEvent();
$CustomRedirectionUrl = $this->_url->getUrl('[ModuleName]/[ModuleName]/[[Action]');
$this->_redirect->setRedirect($CustomRedirectionUrl);
edited Apr 6 '17 at 9:51
answered Apr 5 '17 at 6:38
Qaisar SattiQaisar Satti
27.3k12 gold badges62 silver badges110 bronze badges
27.3k12 gold badges62 silver badges110 bronze badges
I am using above code for redirecting and its working when we exit; after sendResponse() but otherwise its not working. Also i should mention that my observer is observing layout_generate_blocks_after event .
– Vivek Kumar
Apr 5 '17 at 7:15
@Neo i update the code try it and feedback.
– Qaisar Satti
Apr 6 '17 at 9:29
The code you've updated works only for controllers and not for observers
– Vivek Kumar
Apr 6 '17 at 10:24
@Neo i am using this code in observer it is working for me. i am using this eventcontroller_action_predispatch
– Qaisar Satti
Apr 6 '17 at 10:29
1
I've checked properly now and the code is working for me . Thanks
– Vivek Kumar
Apr 6 '17 at 11:27
add a comment |
I am using above code for redirecting and its working when we exit; after sendResponse() but otherwise its not working. Also i should mention that my observer is observing layout_generate_blocks_after event .
– Vivek Kumar
Apr 5 '17 at 7:15
@Neo i update the code try it and feedback.
– Qaisar Satti
Apr 6 '17 at 9:29
The code you've updated works only for controllers and not for observers
– Vivek Kumar
Apr 6 '17 at 10:24
@Neo i am using this code in observer it is working for me. i am using this eventcontroller_action_predispatch
– Qaisar Satti
Apr 6 '17 at 10:29
1
I've checked properly now and the code is working for me . Thanks
– Vivek Kumar
Apr 6 '17 at 11:27
I am using above code for redirecting and its working when we exit; after sendResponse() but otherwise its not working. Also i should mention that my observer is observing layout_generate_blocks_after event .
– Vivek Kumar
Apr 5 '17 at 7:15
I am using above code for redirecting and its working when we exit; after sendResponse() but otherwise its not working. Also i should mention that my observer is observing layout_generate_blocks_after event .
– Vivek Kumar
Apr 5 '17 at 7:15
@Neo i update the code try it and feedback.
– Qaisar Satti
Apr 6 '17 at 9:29
@Neo i update the code try it and feedback.
– Qaisar Satti
Apr 6 '17 at 9:29
The code you've updated works only for controllers and not for observers
– Vivek Kumar
Apr 6 '17 at 10:24
The code you've updated works only for controllers and not for observers
– Vivek Kumar
Apr 6 '17 at 10:24
@Neo i am using this code in observer it is working for me. i am using this event
controller_action_predispatch– Qaisar Satti
Apr 6 '17 at 10:29
@Neo i am using this code in observer it is working for me. i am using this event
controller_action_predispatch– Qaisar Satti
Apr 6 '17 at 10:29
1
1
I've checked properly now and the code is working for me . Thanks
– Vivek Kumar
Apr 6 '17 at 11:27
I've checked properly now and the code is working for me . Thanks
– Vivek Kumar
Apr 6 '17 at 11:27
add a comment |
Without exit or die redirecting to a given URL in observer you need to stop the dispatch event using Action::FLAG_NO_DISPATCH
namespace [Vendor][modulename]Observer;
use MagentoFrameworkEventObserverInterface;
class [YourClass] implements ObserverInterface
protected $urlManager;
protected $actionFlag;
protected $redirect;
public function __construct(
......
MagentoFrameworkUrlInterface $url,
MagentoFrameworkAppActionFlag $actionFlag,
MagentoFrameworkAppResponseRedirectInterface $redirect,
......
)
$this->urlManager = $url;
$this->actionFlag = $actionFlag;
$this->redirect = $redirect;
public function execute(MagentoFrameworkEventObserver $observer)
$controller = $observer->getControllerAction();
// stop the dispatch event.
$this->actionFlag->set('', MagentoFrameworkAppActionAction::FLAG_NO_DISPATCH, true);
//YOUR-ACTION eq to ex. for create acount '*/*/create'
$defaultUrl = $this->urlManager->getUrl('YOUR-ACTION', ['_secure' => true]);
$controller->getResponse()->setRedirect($this->redirect->error($defaultUrl));
I used this code but I am getting following error : Uncaught Error: Call to a member function getResponse()
– David Coder
Dec 12 '18 at 6:06
I am using checkout_cart_product_add_after event
– David Coder
Dec 12 '18 at 6:07
add a comment |
Without exit or die redirecting to a given URL in observer you need to stop the dispatch event using Action::FLAG_NO_DISPATCH
namespace [Vendor][modulename]Observer;
use MagentoFrameworkEventObserverInterface;
class [YourClass] implements ObserverInterface
protected $urlManager;
protected $actionFlag;
protected $redirect;
public function __construct(
......
MagentoFrameworkUrlInterface $url,
MagentoFrameworkAppActionFlag $actionFlag,
MagentoFrameworkAppResponseRedirectInterface $redirect,
......
)
$this->urlManager = $url;
$this->actionFlag = $actionFlag;
$this->redirect = $redirect;
public function execute(MagentoFrameworkEventObserver $observer)
$controller = $observer->getControllerAction();
// stop the dispatch event.
$this->actionFlag->set('', MagentoFrameworkAppActionAction::FLAG_NO_DISPATCH, true);
//YOUR-ACTION eq to ex. for create acount '*/*/create'
$defaultUrl = $this->urlManager->getUrl('YOUR-ACTION', ['_secure' => true]);
$controller->getResponse()->setRedirect($this->redirect->error($defaultUrl));
I used this code but I am getting following error : Uncaught Error: Call to a member function getResponse()
– David Coder
Dec 12 '18 at 6:06
I am using checkout_cart_product_add_after event
– David Coder
Dec 12 '18 at 6:07
add a comment |
Without exit or die redirecting to a given URL in observer you need to stop the dispatch event using Action::FLAG_NO_DISPATCH
namespace [Vendor][modulename]Observer;
use MagentoFrameworkEventObserverInterface;
class [YourClass] implements ObserverInterface
protected $urlManager;
protected $actionFlag;
protected $redirect;
public function __construct(
......
MagentoFrameworkUrlInterface $url,
MagentoFrameworkAppActionFlag $actionFlag,
MagentoFrameworkAppResponseRedirectInterface $redirect,
......
)
$this->urlManager = $url;
$this->actionFlag = $actionFlag;
$this->redirect = $redirect;
public function execute(MagentoFrameworkEventObserver $observer)
$controller = $observer->getControllerAction();
// stop the dispatch event.
$this->actionFlag->set('', MagentoFrameworkAppActionAction::FLAG_NO_DISPATCH, true);
//YOUR-ACTION eq to ex. for create acount '*/*/create'
$defaultUrl = $this->urlManager->getUrl('YOUR-ACTION', ['_secure' => true]);
$controller->getResponse()->setRedirect($this->redirect->error($defaultUrl));
Without exit or die redirecting to a given URL in observer you need to stop the dispatch event using Action::FLAG_NO_DISPATCH
namespace [Vendor][modulename]Observer;
use MagentoFrameworkEventObserverInterface;
class [YourClass] implements ObserverInterface
protected $urlManager;
protected $actionFlag;
protected $redirect;
public function __construct(
......
MagentoFrameworkUrlInterface $url,
MagentoFrameworkAppActionFlag $actionFlag,
MagentoFrameworkAppResponseRedirectInterface $redirect,
......
)
$this->urlManager = $url;
$this->actionFlag = $actionFlag;
$this->redirect = $redirect;
public function execute(MagentoFrameworkEventObserver $observer)
$controller = $observer->getControllerAction();
// stop the dispatch event.
$this->actionFlag->set('', MagentoFrameworkAppActionAction::FLAG_NO_DISPATCH, true);
//YOUR-ACTION eq to ex. for create acount '*/*/create'
$defaultUrl = $this->urlManager->getUrl('YOUR-ACTION', ['_secure' => true]);
$controller->getResponse()->setRedirect($this->redirect->error($defaultUrl));
answered Sep 15 '17 at 10:12
Ankur BhadaniaAnkur Bhadania
1517 bronze badges
1517 bronze badges
I used this code but I am getting following error : Uncaught Error: Call to a member function getResponse()
– David Coder
Dec 12 '18 at 6:06
I am using checkout_cart_product_add_after event
– David Coder
Dec 12 '18 at 6:07
add a comment |
I used this code but I am getting following error : Uncaught Error: Call to a member function getResponse()
– David Coder
Dec 12 '18 at 6:06
I am using checkout_cart_product_add_after event
– David Coder
Dec 12 '18 at 6:07
I used this code but I am getting following error : Uncaught Error: Call to a member function getResponse()
– David Coder
Dec 12 '18 at 6:06
I used this code but I am getting following error : Uncaught Error: Call to a member function getResponse()
– David Coder
Dec 12 '18 at 6:06
I am using checkout_cart_product_add_after event
– David Coder
Dec 12 '18 at 6:07
I am using checkout_cart_product_add_after event
– David Coder
Dec 12 '18 at 6:07
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f167707%2fmagento-2-how-to-redirect-from-a-observer-without-exit-or-die-as-its-not-all%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown