Magento2 : Redirect Page From ObserverMagento 2 - Redirect user to a specific page if not logged inHow can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlMagento2 : Redirect from observermain.CRITICAL: Plugin class doesn't existSet custom price of product when adding to cart code not workingMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderI have created one field using product form field for my price i want save my field value at product creation time from backend magento2Magento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento2: Redirect issue from the Observer

Where are the Wazirs?

What does the multimeter dial do internally?

Is homosexuality or bisexuality allowed for women?

Intern not wearing safety equipment; how could I have handled this differently?

What are the effects of abstaining from eating a certain flavor?

What was the profession 芸者 (female entertainer) called in Britain?

How does one acquire an undead eyeball encased in a gem?

How was the website able to tell my credit card was wrong before it processed it?

Is space division multiplexing really multiplexing?

Findminimum of Integral

How to understand flavors and when to use combination of them?

Gory anime with pink haired girl escaping an asylum

Interpretation of non-significant results as "trends"

Can one block with a protection from color creature?

What are the consequences for a developed nation to not accept any refugees?

QR codes, do people use them?

US citizen traveling with Peruvian passport

Strong Password Detection in Python

Array or vector? Two dimensional array or matrix?

Clarinets in the Rite of Spring

Did depressed people far more accurately estimate how many monsters they killed in a video game?

Deck of Cards with Shuffle and Sort functionality

Publishing papers seem natural to many, while I find it really hard to think novel stuff to pursue till publication. How to cope up with this?

What was the nature of the known bugs in the Space Shuttle software?



Magento2 : Redirect Page From Observer


Magento 2 - Redirect user to a specific page if not logged inHow can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlMagento2 : Redirect from observermain.CRITICAL: Plugin class doesn't existSet custom price of product when adding to cart code not workingMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderI have created one field using product form field for my price i want save my field value at product creation time from backend magento2Magento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento2: Redirect issue from the Observer






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I want to restrict some unwanted users to access the site. So I am redirecting them to 404 Page but my logic is going to an infinite loop as the event I am calling from controller_action_predispatch is called several times and I am not able to find the right one.



My Logic



namespace NamespaceModuleObserver;

class RedirectUsers implements MagentoFrameworkEventObserverInterface

protected $_myHelper;

public function __construct(
NamespaceModuleHelperData $myHelper,
)
$this->_myHelper = $myHelper;


public function execute(MagentoFrameworkEventObserver $observer)
if(in_array($currentUser, $restrictedUsers))
$url = $this->_myHelper->getNoRoutePageUrl();
header('Location: ' . $url);
exit;





My event.xml



<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_action_predispatch">
<observer instance="NamespaceModuleObserverRedirectIp" name="namespace_module_observer_controller_actionpredispatch_controller_action_predispatch"/>
</event>
</config>









share|improve this question
























  • Should read more here: magento.stackexchange.com/questions/131706/…

    – Khoa TruongDinh
    Oct 15 '16 at 5:29

















0















I want to restrict some unwanted users to access the site. So I am redirecting them to 404 Page but my logic is going to an infinite loop as the event I am calling from controller_action_predispatch is called several times and I am not able to find the right one.



My Logic



namespace NamespaceModuleObserver;

class RedirectUsers implements MagentoFrameworkEventObserverInterface

protected $_myHelper;

public function __construct(
NamespaceModuleHelperData $myHelper,
)
$this->_myHelper = $myHelper;


public function execute(MagentoFrameworkEventObserver $observer)
if(in_array($currentUser, $restrictedUsers))
$url = $this->_myHelper->getNoRoutePageUrl();
header('Location: ' . $url);
exit;





My event.xml



<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_action_predispatch">
<observer instance="NamespaceModuleObserverRedirectIp" name="namespace_module_observer_controller_actionpredispatch_controller_action_predispatch"/>
</event>
</config>









share|improve this question
























  • Should read more here: magento.stackexchange.com/questions/131706/…

    – Khoa TruongDinh
    Oct 15 '16 at 5:29













0












0








0








I want to restrict some unwanted users to access the site. So I am redirecting them to 404 Page but my logic is going to an infinite loop as the event I am calling from controller_action_predispatch is called several times and I am not able to find the right one.



My Logic



namespace NamespaceModuleObserver;

class RedirectUsers implements MagentoFrameworkEventObserverInterface

protected $_myHelper;

public function __construct(
NamespaceModuleHelperData $myHelper,
)
$this->_myHelper = $myHelper;


public function execute(MagentoFrameworkEventObserver $observer)
if(in_array($currentUser, $restrictedUsers))
$url = $this->_myHelper->getNoRoutePageUrl();
header('Location: ' . $url);
exit;





My event.xml



<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_action_predispatch">
<observer instance="NamespaceModuleObserverRedirectIp" name="namespace_module_observer_controller_actionpredispatch_controller_action_predispatch"/>
</event>
</config>









share|improve this question
















I want to restrict some unwanted users to access the site. So I am redirecting them to 404 Page but my logic is going to an infinite loop as the event I am calling from controller_action_predispatch is called several times and I am not able to find the right one.



My Logic



namespace NamespaceModuleObserver;

class RedirectUsers implements MagentoFrameworkEventObserverInterface

protected $_myHelper;

public function __construct(
NamespaceModuleHelperData $myHelper,
)
$this->_myHelper = $myHelper;


public function execute(MagentoFrameworkEventObserver $observer)
if(in_array($currentUser, $restrictedUsers))
$url = $this->_myHelper->getNoRoutePageUrl();
header('Location: ' . $url);
exit;





My event.xml



<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_action_predispatch">
<observer instance="NamespaceModuleObserverRedirectIp" name="namespace_module_observer_controller_actionpredispatch_controller_action_predispatch"/>
</event>
</config>






magento2 event-observer redirect






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 29 at 5:39









Sarfaraj Sipai

5211 gold badge7 silver badges20 bronze badges




5211 gold badge7 silver badges20 bronze badges










asked Oct 14 '16 at 9:45









PriyankPriyank

5,7024 gold badges24 silver badges53 bronze badges




5,7024 gold badges24 silver badges53 bronze badges












  • Should read more here: magento.stackexchange.com/questions/131706/…

    – Khoa TruongDinh
    Oct 15 '16 at 5:29

















  • Should read more here: magento.stackexchange.com/questions/131706/…

    – Khoa TruongDinh
    Oct 15 '16 at 5:29
















Should read more here: magento.stackexchange.com/questions/131706/…

– Khoa TruongDinh
Oct 15 '16 at 5:29





Should read more here: magento.stackexchange.com/questions/131706/…

– Khoa TruongDinh
Oct 15 '16 at 5:29










2 Answers
2






active

oldest

votes


















0














Below one you can follow



namespace CustomModuleObserver;

use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkAppRequestDataPersistorInterface;
use MagentoFrameworkAppObjectManager;

class YourObserver implements ObserverInterface

/**
* @var MagentoFrameworkAppResponseRedirectInterface
*/
protected $redirect;

public function __construct(MagentoFrameworkAppResponseRedirectInterface $redirect)
$this->redirect = $redirect;


public function execute(MagentoFrameworkEventObserver $observer)

$actionName = $observer->getEvent()->getRequest()->getFullActionName();
$controller = $observer->getControllerAction();

$this->redirect->redirect($controller->getResponse(), $this->redirect->getRefererUrl());







share|improve this answer























  • Can you explain what is it doing or how I should implement this

    – Priyank
    Oct 14 '16 at 10:05











  • Copy this code & put ur things in it Replace $this->redirect->getRefererUrl() with Ur URL which you want to redirect

    – Ankit Shah
    Oct 14 '16 at 10:15











  • I hope you understood my question. I am not asking how I should redirect. I am asking my redirection is going to infinite loop as the event get calls again and again. I tried your example and I am getting the same result

    – Priyank
    Oct 14 '16 at 10:22











  • Try to remove ur condition & Check. Please also give Ur events.xml file as well

    – Ankit Shah
    Oct 14 '16 at 10:32











  • event xml added

    – Priyank
    Oct 14 '16 at 10:37


















0














set obeject :



//@var MagentoBackendModelViewResultRedirect $resultRedirect 
$resultRedirect = $this->resultRedirectFactory->create();


then just use setPath() method



return $resultRedirect->setPath('*/*/');





share|improve this answer

























    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
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f140868%2fmagento2-redirect-page-from-observer%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









    0














    Below one you can follow



    namespace CustomModuleObserver;

    use MagentoFrameworkEventObserverInterface;
    use MagentoFrameworkAppRequestDataPersistorInterface;
    use MagentoFrameworkAppObjectManager;

    class YourObserver implements ObserverInterface

    /**
    * @var MagentoFrameworkAppResponseRedirectInterface
    */
    protected $redirect;

    public function __construct(MagentoFrameworkAppResponseRedirectInterface $redirect)
    $this->redirect = $redirect;


    public function execute(MagentoFrameworkEventObserver $observer)

    $actionName = $observer->getEvent()->getRequest()->getFullActionName();
    $controller = $observer->getControllerAction();

    $this->redirect->redirect($controller->getResponse(), $this->redirect->getRefererUrl());







    share|improve this answer























    • Can you explain what is it doing or how I should implement this

      – Priyank
      Oct 14 '16 at 10:05











    • Copy this code & put ur things in it Replace $this->redirect->getRefererUrl() with Ur URL which you want to redirect

      – Ankit Shah
      Oct 14 '16 at 10:15











    • I hope you understood my question. I am not asking how I should redirect. I am asking my redirection is going to infinite loop as the event get calls again and again. I tried your example and I am getting the same result

      – Priyank
      Oct 14 '16 at 10:22











    • Try to remove ur condition & Check. Please also give Ur events.xml file as well

      – Ankit Shah
      Oct 14 '16 at 10:32











    • event xml added

      – Priyank
      Oct 14 '16 at 10:37















    0














    Below one you can follow



    namespace CustomModuleObserver;

    use MagentoFrameworkEventObserverInterface;
    use MagentoFrameworkAppRequestDataPersistorInterface;
    use MagentoFrameworkAppObjectManager;

    class YourObserver implements ObserverInterface

    /**
    * @var MagentoFrameworkAppResponseRedirectInterface
    */
    protected $redirect;

    public function __construct(MagentoFrameworkAppResponseRedirectInterface $redirect)
    $this->redirect = $redirect;


    public function execute(MagentoFrameworkEventObserver $observer)

    $actionName = $observer->getEvent()->getRequest()->getFullActionName();
    $controller = $observer->getControllerAction();

    $this->redirect->redirect($controller->getResponse(), $this->redirect->getRefererUrl());







    share|improve this answer























    • Can you explain what is it doing or how I should implement this

      – Priyank
      Oct 14 '16 at 10:05











    • Copy this code & put ur things in it Replace $this->redirect->getRefererUrl() with Ur URL which you want to redirect

      – Ankit Shah
      Oct 14 '16 at 10:15











    • I hope you understood my question. I am not asking how I should redirect. I am asking my redirection is going to infinite loop as the event get calls again and again. I tried your example and I am getting the same result

      – Priyank
      Oct 14 '16 at 10:22











    • Try to remove ur condition & Check. Please also give Ur events.xml file as well

      – Ankit Shah
      Oct 14 '16 at 10:32











    • event xml added

      – Priyank
      Oct 14 '16 at 10:37













    0












    0








    0







    Below one you can follow



    namespace CustomModuleObserver;

    use MagentoFrameworkEventObserverInterface;
    use MagentoFrameworkAppRequestDataPersistorInterface;
    use MagentoFrameworkAppObjectManager;

    class YourObserver implements ObserverInterface

    /**
    * @var MagentoFrameworkAppResponseRedirectInterface
    */
    protected $redirect;

    public function __construct(MagentoFrameworkAppResponseRedirectInterface $redirect)
    $this->redirect = $redirect;


    public function execute(MagentoFrameworkEventObserver $observer)

    $actionName = $observer->getEvent()->getRequest()->getFullActionName();
    $controller = $observer->getControllerAction();

    $this->redirect->redirect($controller->getResponse(), $this->redirect->getRefererUrl());







    share|improve this answer













    Below one you can follow



    namespace CustomModuleObserver;

    use MagentoFrameworkEventObserverInterface;
    use MagentoFrameworkAppRequestDataPersistorInterface;
    use MagentoFrameworkAppObjectManager;

    class YourObserver implements ObserverInterface

    /**
    * @var MagentoFrameworkAppResponseRedirectInterface
    */
    protected $redirect;

    public function __construct(MagentoFrameworkAppResponseRedirectInterface $redirect)
    $this->redirect = $redirect;


    public function execute(MagentoFrameworkEventObserver $observer)

    $actionName = $observer->getEvent()->getRequest()->getFullActionName();
    $controller = $observer->getControllerAction();

    $this->redirect->redirect($controller->getResponse(), $this->redirect->getRefererUrl());








    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Oct 14 '16 at 9:56









    Ankit ShahAnkit Shah

    4,99912 gold badges75 silver badges148 bronze badges




    4,99912 gold badges75 silver badges148 bronze badges












    • Can you explain what is it doing or how I should implement this

      – Priyank
      Oct 14 '16 at 10:05











    • Copy this code & put ur things in it Replace $this->redirect->getRefererUrl() with Ur URL which you want to redirect

      – Ankit Shah
      Oct 14 '16 at 10:15











    • I hope you understood my question. I am not asking how I should redirect. I am asking my redirection is going to infinite loop as the event get calls again and again. I tried your example and I am getting the same result

      – Priyank
      Oct 14 '16 at 10:22











    • Try to remove ur condition & Check. Please also give Ur events.xml file as well

      – Ankit Shah
      Oct 14 '16 at 10:32











    • event xml added

      – Priyank
      Oct 14 '16 at 10:37

















    • Can you explain what is it doing or how I should implement this

      – Priyank
      Oct 14 '16 at 10:05











    • Copy this code & put ur things in it Replace $this->redirect->getRefererUrl() with Ur URL which you want to redirect

      – Ankit Shah
      Oct 14 '16 at 10:15











    • I hope you understood my question. I am not asking how I should redirect. I am asking my redirection is going to infinite loop as the event get calls again and again. I tried your example and I am getting the same result

      – Priyank
      Oct 14 '16 at 10:22











    • Try to remove ur condition & Check. Please also give Ur events.xml file as well

      – Ankit Shah
      Oct 14 '16 at 10:32











    • event xml added

      – Priyank
      Oct 14 '16 at 10:37
















    Can you explain what is it doing or how I should implement this

    – Priyank
    Oct 14 '16 at 10:05





    Can you explain what is it doing or how I should implement this

    – Priyank
    Oct 14 '16 at 10:05













    Copy this code & put ur things in it Replace $this->redirect->getRefererUrl() with Ur URL which you want to redirect

    – Ankit Shah
    Oct 14 '16 at 10:15





    Copy this code & put ur things in it Replace $this->redirect->getRefererUrl() with Ur URL which you want to redirect

    – Ankit Shah
    Oct 14 '16 at 10:15













    I hope you understood my question. I am not asking how I should redirect. I am asking my redirection is going to infinite loop as the event get calls again and again. I tried your example and I am getting the same result

    – Priyank
    Oct 14 '16 at 10:22





    I hope you understood my question. I am not asking how I should redirect. I am asking my redirection is going to infinite loop as the event get calls again and again. I tried your example and I am getting the same result

    – Priyank
    Oct 14 '16 at 10:22













    Try to remove ur condition & Check. Please also give Ur events.xml file as well

    – Ankit Shah
    Oct 14 '16 at 10:32





    Try to remove ur condition & Check. Please also give Ur events.xml file as well

    – Ankit Shah
    Oct 14 '16 at 10:32













    event xml added

    – Priyank
    Oct 14 '16 at 10:37





    event xml added

    – Priyank
    Oct 14 '16 at 10:37













    0














    set obeject :



    //@var MagentoBackendModelViewResultRedirect $resultRedirect 
    $resultRedirect = $this->resultRedirectFactory->create();


    then just use setPath() method



    return $resultRedirect->setPath('*/*/');





    share|improve this answer



























      0














      set obeject :



      //@var MagentoBackendModelViewResultRedirect $resultRedirect 
      $resultRedirect = $this->resultRedirectFactory->create();


      then just use setPath() method



      return $resultRedirect->setPath('*/*/');





      share|improve this answer

























        0












        0








        0







        set obeject :



        //@var MagentoBackendModelViewResultRedirect $resultRedirect 
        $resultRedirect = $this->resultRedirectFactory->create();


        then just use setPath() method



        return $resultRedirect->setPath('*/*/');





        share|improve this answer













        set obeject :



        //@var MagentoBackendModelViewResultRedirect $resultRedirect 
        $resultRedirect = $this->resultRedirectFactory->create();


        then just use setPath() method



        return $resultRedirect->setPath('*/*/');






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Oct 14 '16 at 13:23









        Ronak ChauhanRonak Chauhan

        4,3941 gold badge17 silver badges51 bronze badges




        4,3941 gold badge17 silver badges51 bronze badges



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f140868%2fmagento2-redirect-page-from-observer%23new-answer', 'question_page');

            );

            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







            Popular posts from this blog

            Grendel Contents Story Scholarship Depictions Notes References Navigation menu10.1093/notesj/gjn112Berserkeree

            Area configuration aggregation error after install Porto themeMagento 2.1 CE Installed but front/backend not loading/workingCSS not loading on page within Magento 2 pageCannot install module in Magento 2no commands defined in the “setup” namespace. in Magento2Magento 2: Static files are present but shows 404Why do i have to always run the commands to clean cache in Magento 2.1.8?Failure reason: 'Unable to unserialize value.'Error 500 after magento migrationIn production mode the site does not loadMagento 2 : Error 500 after installing

            Middle Expansion Olielle Resaix Definition: Uttering songs of triumph shouting with joy triumphant exulting Sejunction Journal 붙다 달 고급 품목 외출 The stretch trades the screeching tin. Definition: The act of speaking with a drawl a drawl Cough Sand Definition: An uproar a quarrel a noisy outbreak Shake Iron Publicize Horse House Baby 사과 Resaix Flaggy Jelly Temporary Unequaled Puppet A drop in the bucket Shrew 성격 회원 성질 미팅 The burn frames the tacky quality. Materialistic The smoke reduces the way. Yammoe Nondescript Cheek 얼굴 배 약하다 날리다 타다 The illegal country shows the iron. Help Rule Drearien Smoke Teaching Meaty Wasp Abraham Lincoln Jaws 진심 수리하다 Size Cork Idea Convert Think Lark John Lennon 거울 청소 군 추천하다 아이스크림