How to redirect customer from controller not observer to login page magento 2

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





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







up vote
2
down vote

favorite












I want to redirect customer to login page in my controller without using observer.
here is my code



 protected $urlInterface;

public function __construct(

MagentoFrameworkAppActionContext $context,
MagentoFrameworkAppResponseRedirectInterface $redirect,
MagentoFrameworkUrlInterface $url,
MagentoFrameworkAppResourceConnection $resource,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoCustomerModelSession $customerSession,
MagenticiansEvaluationModelEvaluationFactory $EvaluationFactory,
MagenticiansEvaluationModelEvaluation $eval,
MagenticiansEvaluationModelResourceModelEvaluationCollection $evaluationCollection,
MagentoFrameworkControllerResultFactory $result)

parent::__construct(
$context
);
$this->EvaluationFactory = $EvaluationFactory;
$this->_customerSession = $customerSession;
$this->_storeManager = $storeManager;
$this->resultRedirect = $result;
$this->_resource = $resource;
$this->_evaluation =$eval;
$this->_evaluationCollection = $evaluationCollection;
$this->redirect = $redirect;
$this->urlInterface = $url;






// Create login URL
$login_url = $this->urlInterface
->getUrl('customer/account/login');

$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setUrl($login_url);
$this->_view->loadLayout();
$this->_view->getLayout()->initMessages();
$this->_view->renderLayout();


but as result i have no redirection response please help me and thanks in advance










share|improve this question





























    up vote
    2
    down vote

    favorite












    I want to redirect customer to login page in my controller without using observer.
    here is my code



     protected $urlInterface;

    public function __construct(

    MagentoFrameworkAppActionContext $context,
    MagentoFrameworkAppResponseRedirectInterface $redirect,
    MagentoFrameworkUrlInterface $url,
    MagentoFrameworkAppResourceConnection $resource,
    MagentoStoreModelStoreManagerInterface $storeManager,
    MagentoCustomerModelSession $customerSession,
    MagenticiansEvaluationModelEvaluationFactory $EvaluationFactory,
    MagenticiansEvaluationModelEvaluation $eval,
    MagenticiansEvaluationModelResourceModelEvaluationCollection $evaluationCollection,
    MagentoFrameworkControllerResultFactory $result)

    parent::__construct(
    $context
    );
    $this->EvaluationFactory = $EvaluationFactory;
    $this->_customerSession = $customerSession;
    $this->_storeManager = $storeManager;
    $this->resultRedirect = $result;
    $this->_resource = $resource;
    $this->_evaluation =$eval;
    $this->_evaluationCollection = $evaluationCollection;
    $this->redirect = $redirect;
    $this->urlInterface = $url;






    // Create login URL
    $login_url = $this->urlInterface
    ->getUrl('customer/account/login');

    $resultRedirect = $this->resultRedirectFactory->create();
    $resultRedirect->setUrl($login_url);
    $this->_view->loadLayout();
    $this->_view->getLayout()->initMessages();
    $this->_view->renderLayout();


    but as result i have no redirection response please help me and thanks in advance










    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I want to redirect customer to login page in my controller without using observer.
      here is my code



       protected $urlInterface;

      public function __construct(

      MagentoFrameworkAppActionContext $context,
      MagentoFrameworkAppResponseRedirectInterface $redirect,
      MagentoFrameworkUrlInterface $url,
      MagentoFrameworkAppResourceConnection $resource,
      MagentoStoreModelStoreManagerInterface $storeManager,
      MagentoCustomerModelSession $customerSession,
      MagenticiansEvaluationModelEvaluationFactory $EvaluationFactory,
      MagenticiansEvaluationModelEvaluation $eval,
      MagenticiansEvaluationModelResourceModelEvaluationCollection $evaluationCollection,
      MagentoFrameworkControllerResultFactory $result)

      parent::__construct(
      $context
      );
      $this->EvaluationFactory = $EvaluationFactory;
      $this->_customerSession = $customerSession;
      $this->_storeManager = $storeManager;
      $this->resultRedirect = $result;
      $this->_resource = $resource;
      $this->_evaluation =$eval;
      $this->_evaluationCollection = $evaluationCollection;
      $this->redirect = $redirect;
      $this->urlInterface = $url;






      // Create login URL
      $login_url = $this->urlInterface
      ->getUrl('customer/account/login');

      $resultRedirect = $this->resultRedirectFactory->create();
      $resultRedirect->setUrl($login_url);
      $this->_view->loadLayout();
      $this->_view->getLayout()->initMessages();
      $this->_view->renderLayout();


      but as result i have no redirection response please help me and thanks in advance










      share|improve this question















      I want to redirect customer to login page in my controller without using observer.
      here is my code



       protected $urlInterface;

      public function __construct(

      MagentoFrameworkAppActionContext $context,
      MagentoFrameworkAppResponseRedirectInterface $redirect,
      MagentoFrameworkUrlInterface $url,
      MagentoFrameworkAppResourceConnection $resource,
      MagentoStoreModelStoreManagerInterface $storeManager,
      MagentoCustomerModelSession $customerSession,
      MagenticiansEvaluationModelEvaluationFactory $EvaluationFactory,
      MagenticiansEvaluationModelEvaluation $eval,
      MagenticiansEvaluationModelResourceModelEvaluationCollection $evaluationCollection,
      MagentoFrameworkControllerResultFactory $result)

      parent::__construct(
      $context
      );
      $this->EvaluationFactory = $EvaluationFactory;
      $this->_customerSession = $customerSession;
      $this->_storeManager = $storeManager;
      $this->resultRedirect = $result;
      $this->_resource = $resource;
      $this->_evaluation =$eval;
      $this->_evaluationCollection = $evaluationCollection;
      $this->redirect = $redirect;
      $this->urlInterface = $url;






      // Create login URL
      $login_url = $this->urlInterface
      ->getUrl('customer/account/login');

      $resultRedirect = $this->resultRedirectFactory->create();
      $resultRedirect->setUrl($login_url);
      $this->_view->loadLayout();
      $this->_view->getLayout()->initMessages();
      $this->_view->renderLayout();


      but as result i have no redirection response please help me and thanks in advance







      magento2 login






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 4 hours ago









      Amit Bera♦

      54k1366157




      54k1366157










      asked 4 hours ago









      Developper Magento

      977




      977




















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Your code should be looking like something below, your controller is missing the execute() method which is the entry point for the controller action in Magento 2 and return statement which returns the redirection url. Your code should look like something below:



          <?php

          namespace YourNameSpace;

          class YourClassName

          protected $urlInterface;

          public function __construct(

          MagentoFrameworkAppActionContext $context,
          MagentoFrameworkAppResponseRedirectInterface $redirect,
          MagentoFrameworkUrlInterface $url,
          MagentoFrameworkAppResourceConnection $resource,
          MagentoStoreModelStoreManagerInterface $storeManager,
          MagentoCustomerModelSession $customerSession,
          MagenticiansEvaluationModelEvaluationFactory $EvaluationFactory,
          MagenticiansEvaluationModelEvaluation $eval,
          MagenticiansEvaluationModelResourceModelEvaluationCollection $evaluationCollection,
          MagentoFrameworkControllerResultFactory $result)

          parent::__construct(
          $context
          );
          $this->EvaluationFactory = $EvaluationFactory;
          $this->_customerSession = $customerSession;
          $this->_storeManager = $storeManager;
          $this->resultRedirect = $result;
          $this->_resource = $resource;
          $this->_evaluation =$eval;
          $this->_evaluationCollection = $evaluationCollection;
          $this->redirect = $redirect;
          $this->urlInterface = $url;





          public function execute()

          // Create login URL
          $login_url = $this->urlInterface
          ->getUrl('customer/account/login');
          return $this->resultRedirectFactory->create()->setPath($login_url);




          ?>





          share|improve this answer



























            up vote
            2
            down vote













            Try something like this



            return $this->resultRedirectFactory->create()->setPath('customer/account/login');


            You need to return the ResultRedirect object inside your execute() method






            share|improve this answer



























              up vote
              1
              down vote













              You have to redirection from execute(), in your code . You did not build Action class properly.



              And your code it is missing this function.



              public function execute()


              return $this->resultRedirectFactory->create()->setPath('customer/account/login');






              share|improve this answer





























                up vote
                1
                down vote













                Use below code



                <?php
                namespace VenorModuleControllerIndex;
                use MagentoFrameworkViewResultPageFactory;
                use MagentoFrameworkAppActionContext;
                use MagentoCustomerModelSession;
                use MagentoFrameworkAppResponseHttp;
                use MagentoFrameworkUrlInterface;

                class Index extends MagentoFrameworkAppActionAction

                public function __construct(

                MagentoFrameworkAppActionContext $context,
                MagentoFrameworkAppResponseRedirectInterface $redirect,
                MagentoFrameworkUrlInterface $url,
                MagentoFrameworkAppResourceConnection $resource,
                MagentoStoreModelStoreManagerInterface $storeManager,
                MagentoCustomerModelSession $customerSession,
                MagenticiansEvaluationModelEvaluationFactory $EvaluationFactory,
                MagenticiansEvaluationModelEvaluation $eval,
                MagenticiansEvaluationModelResourceModelEvaluationCollection $evaluationCollection,
                MagentoFrameworkControllerResultFactory $result,
                MagentoFrameworkViewResultPageFactory $pageFactory,
                MagentoFrameworkAppResponseHttp $http

                )

                parent::__construct(
                $context
                );
                $this->EvaluationFactory = $EvaluationFactory;
                $this->_customerSession = $customerSession;
                $this->_storeManager = $storeManager;
                $this->resultRedirect = $result;
                $this->_resource = $resource;
                $this->_evaluation =$eval;
                $this->_evaluationCollection = $evaluationCollection;
                $this->redirect = $redirect;
                $this->urlInterface = $url;
                $this->_pageFactory = $pageFactory;
                $this->_http = $http;




                public function execute()

                $login_url = $this->urlInterface->getUrl('customer/account/login');
                $this->_http->setRedirect($login_url, 301);
                $pageFactory = $this->_pageFactory->create();
                return $pageFactory;








                share|improve this answer






















                • <b>Fatal error</b>: Uncaught Error: Call to a member function setRedirect() on null in /var/www/html/sajjada/app/code/Magenticians/Evaluation/Controller/Index/Index.php:123
                  – Developper Magento
                  4 hours ago










                • did you do use MagentoFrameworkUrlInterface; in top and Http $http, in construnction and $this->_http = $http;
                  – Ansar Husain
                  4 hours ago










                • sorry i forget to add $this->_http = $http; but even added this line there is no redirection to login page but error is gone so what s the problem can you please help me
                  – Developper Magento
                  4 hours ago










                • Check post now i have updated code using your code in my answer use as it is
                  – Ansar Husain
                  3 hours ago











                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',
                convertImagesToLinks: false,
                noModals: false,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                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%2f242273%2fhow-to-redirect-customer-from-controller-not-observer-to-login-page-magento-2%23new-answer', 'question_page');

                );

                Post as a guest






























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                1
                down vote



                accepted










                Your code should be looking like something below, your controller is missing the execute() method which is the entry point for the controller action in Magento 2 and return statement which returns the redirection url. Your code should look like something below:



                <?php

                namespace YourNameSpace;

                class YourClassName

                protected $urlInterface;

                public function __construct(

                MagentoFrameworkAppActionContext $context,
                MagentoFrameworkAppResponseRedirectInterface $redirect,
                MagentoFrameworkUrlInterface $url,
                MagentoFrameworkAppResourceConnection $resource,
                MagentoStoreModelStoreManagerInterface $storeManager,
                MagentoCustomerModelSession $customerSession,
                MagenticiansEvaluationModelEvaluationFactory $EvaluationFactory,
                MagenticiansEvaluationModelEvaluation $eval,
                MagenticiansEvaluationModelResourceModelEvaluationCollection $evaluationCollection,
                MagentoFrameworkControllerResultFactory $result)

                parent::__construct(
                $context
                );
                $this->EvaluationFactory = $EvaluationFactory;
                $this->_customerSession = $customerSession;
                $this->_storeManager = $storeManager;
                $this->resultRedirect = $result;
                $this->_resource = $resource;
                $this->_evaluation =$eval;
                $this->_evaluationCollection = $evaluationCollection;
                $this->redirect = $redirect;
                $this->urlInterface = $url;





                public function execute()

                // Create login URL
                $login_url = $this->urlInterface
                ->getUrl('customer/account/login');
                return $this->resultRedirectFactory->create()->setPath($login_url);




                ?>





                share|improve this answer
























                  up vote
                  1
                  down vote



                  accepted










                  Your code should be looking like something below, your controller is missing the execute() method which is the entry point for the controller action in Magento 2 and return statement which returns the redirection url. Your code should look like something below:



                  <?php

                  namespace YourNameSpace;

                  class YourClassName

                  protected $urlInterface;

                  public function __construct(

                  MagentoFrameworkAppActionContext $context,
                  MagentoFrameworkAppResponseRedirectInterface $redirect,
                  MagentoFrameworkUrlInterface $url,
                  MagentoFrameworkAppResourceConnection $resource,
                  MagentoStoreModelStoreManagerInterface $storeManager,
                  MagentoCustomerModelSession $customerSession,
                  MagenticiansEvaluationModelEvaluationFactory $EvaluationFactory,
                  MagenticiansEvaluationModelEvaluation $eval,
                  MagenticiansEvaluationModelResourceModelEvaluationCollection $evaluationCollection,
                  MagentoFrameworkControllerResultFactory $result)

                  parent::__construct(
                  $context
                  );
                  $this->EvaluationFactory = $EvaluationFactory;
                  $this->_customerSession = $customerSession;
                  $this->_storeManager = $storeManager;
                  $this->resultRedirect = $result;
                  $this->_resource = $resource;
                  $this->_evaluation =$eval;
                  $this->_evaluationCollection = $evaluationCollection;
                  $this->redirect = $redirect;
                  $this->urlInterface = $url;





                  public function execute()

                  // Create login URL
                  $login_url = $this->urlInterface
                  ->getUrl('customer/account/login');
                  return $this->resultRedirectFactory->create()->setPath($login_url);




                  ?>





                  share|improve this answer






















                    up vote
                    1
                    down vote



                    accepted







                    up vote
                    1
                    down vote



                    accepted






                    Your code should be looking like something below, your controller is missing the execute() method which is the entry point for the controller action in Magento 2 and return statement which returns the redirection url. Your code should look like something below:



                    <?php

                    namespace YourNameSpace;

                    class YourClassName

                    protected $urlInterface;

                    public function __construct(

                    MagentoFrameworkAppActionContext $context,
                    MagentoFrameworkAppResponseRedirectInterface $redirect,
                    MagentoFrameworkUrlInterface $url,
                    MagentoFrameworkAppResourceConnection $resource,
                    MagentoStoreModelStoreManagerInterface $storeManager,
                    MagentoCustomerModelSession $customerSession,
                    MagenticiansEvaluationModelEvaluationFactory $EvaluationFactory,
                    MagenticiansEvaluationModelEvaluation $eval,
                    MagenticiansEvaluationModelResourceModelEvaluationCollection $evaluationCollection,
                    MagentoFrameworkControllerResultFactory $result)

                    parent::__construct(
                    $context
                    );
                    $this->EvaluationFactory = $EvaluationFactory;
                    $this->_customerSession = $customerSession;
                    $this->_storeManager = $storeManager;
                    $this->resultRedirect = $result;
                    $this->_resource = $resource;
                    $this->_evaluation =$eval;
                    $this->_evaluationCollection = $evaluationCollection;
                    $this->redirect = $redirect;
                    $this->urlInterface = $url;





                    public function execute()

                    // Create login URL
                    $login_url = $this->urlInterface
                    ->getUrl('customer/account/login');
                    return $this->resultRedirectFactory->create()->setPath($login_url);




                    ?>





                    share|improve this answer












                    Your code should be looking like something below, your controller is missing the execute() method which is the entry point for the controller action in Magento 2 and return statement which returns the redirection url. Your code should look like something below:



                    <?php

                    namespace YourNameSpace;

                    class YourClassName

                    protected $urlInterface;

                    public function __construct(

                    MagentoFrameworkAppActionContext $context,
                    MagentoFrameworkAppResponseRedirectInterface $redirect,
                    MagentoFrameworkUrlInterface $url,
                    MagentoFrameworkAppResourceConnection $resource,
                    MagentoStoreModelStoreManagerInterface $storeManager,
                    MagentoCustomerModelSession $customerSession,
                    MagenticiansEvaluationModelEvaluationFactory $EvaluationFactory,
                    MagenticiansEvaluationModelEvaluation $eval,
                    MagenticiansEvaluationModelResourceModelEvaluationCollection $evaluationCollection,
                    MagentoFrameworkControllerResultFactory $result)

                    parent::__construct(
                    $context
                    );
                    $this->EvaluationFactory = $EvaluationFactory;
                    $this->_customerSession = $customerSession;
                    $this->_storeManager = $storeManager;
                    $this->resultRedirect = $result;
                    $this->_resource = $resource;
                    $this->_evaluation =$eval;
                    $this->_evaluationCollection = $evaluationCollection;
                    $this->redirect = $redirect;
                    $this->urlInterface = $url;





                    public function execute()

                    // Create login URL
                    $login_url = $this->urlInterface
                    ->getUrl('customer/account/login');
                    return $this->resultRedirectFactory->create()->setPath($login_url);




                    ?>






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 4 hours ago









                    Sourabh Kumar Sharma

                    15111




                    15111






















                        up vote
                        2
                        down vote













                        Try something like this



                        return $this->resultRedirectFactory->create()->setPath('customer/account/login');


                        You need to return the ResultRedirect object inside your execute() method






                        share|improve this answer
























                          up vote
                          2
                          down vote













                          Try something like this



                          return $this->resultRedirectFactory->create()->setPath('customer/account/login');


                          You need to return the ResultRedirect object inside your execute() method






                          share|improve this answer






















                            up vote
                            2
                            down vote










                            up vote
                            2
                            down vote









                            Try something like this



                            return $this->resultRedirectFactory->create()->setPath('customer/account/login');


                            You need to return the ResultRedirect object inside your execute() method






                            share|improve this answer












                            Try something like this



                            return $this->resultRedirectFactory->create()->setPath('customer/account/login');


                            You need to return the ResultRedirect object inside your execute() method







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 4 hours ago









                            George M

                            2965




                            2965




















                                up vote
                                1
                                down vote













                                You have to redirection from execute(), in your code . You did not build Action class properly.



                                And your code it is missing this function.



                                public function execute()


                                return $this->resultRedirectFactory->create()->setPath('customer/account/login');






                                share|improve this answer


























                                  up vote
                                  1
                                  down vote













                                  You have to redirection from execute(), in your code . You did not build Action class properly.



                                  And your code it is missing this function.



                                  public function execute()


                                  return $this->resultRedirectFactory->create()->setPath('customer/account/login');






                                  share|improve this answer
























                                    up vote
                                    1
                                    down vote










                                    up vote
                                    1
                                    down vote









                                    You have to redirection from execute(), in your code . You did not build Action class properly.



                                    And your code it is missing this function.



                                    public function execute()


                                    return $this->resultRedirectFactory->create()->setPath('customer/account/login');






                                    share|improve this answer














                                    You have to redirection from execute(), in your code . You did not build Action class properly.



                                    And your code it is missing this function.



                                    public function execute()


                                    return $this->resultRedirectFactory->create()->setPath('customer/account/login');







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited 4 hours ago

























                                    answered 4 hours ago









                                    Amit Bera♦

                                    54k1366157




                                    54k1366157




















                                        up vote
                                        1
                                        down vote













                                        Use below code



                                        <?php
                                        namespace VenorModuleControllerIndex;
                                        use MagentoFrameworkViewResultPageFactory;
                                        use MagentoFrameworkAppActionContext;
                                        use MagentoCustomerModelSession;
                                        use MagentoFrameworkAppResponseHttp;
                                        use MagentoFrameworkUrlInterface;

                                        class Index extends MagentoFrameworkAppActionAction

                                        public function __construct(

                                        MagentoFrameworkAppActionContext $context,
                                        MagentoFrameworkAppResponseRedirectInterface $redirect,
                                        MagentoFrameworkUrlInterface $url,
                                        MagentoFrameworkAppResourceConnection $resource,
                                        MagentoStoreModelStoreManagerInterface $storeManager,
                                        MagentoCustomerModelSession $customerSession,
                                        MagenticiansEvaluationModelEvaluationFactory $EvaluationFactory,
                                        MagenticiansEvaluationModelEvaluation $eval,
                                        MagenticiansEvaluationModelResourceModelEvaluationCollection $evaluationCollection,
                                        MagentoFrameworkControllerResultFactory $result,
                                        MagentoFrameworkViewResultPageFactory $pageFactory,
                                        MagentoFrameworkAppResponseHttp $http

                                        )

                                        parent::__construct(
                                        $context
                                        );
                                        $this->EvaluationFactory = $EvaluationFactory;
                                        $this->_customerSession = $customerSession;
                                        $this->_storeManager = $storeManager;
                                        $this->resultRedirect = $result;
                                        $this->_resource = $resource;
                                        $this->_evaluation =$eval;
                                        $this->_evaluationCollection = $evaluationCollection;
                                        $this->redirect = $redirect;
                                        $this->urlInterface = $url;
                                        $this->_pageFactory = $pageFactory;
                                        $this->_http = $http;




                                        public function execute()

                                        $login_url = $this->urlInterface->getUrl('customer/account/login');
                                        $this->_http->setRedirect($login_url, 301);
                                        $pageFactory = $this->_pageFactory->create();
                                        return $pageFactory;








                                        share|improve this answer






















                                        • <b>Fatal error</b>: Uncaught Error: Call to a member function setRedirect() on null in /var/www/html/sajjada/app/code/Magenticians/Evaluation/Controller/Index/Index.php:123
                                          – Developper Magento
                                          4 hours ago










                                        • did you do use MagentoFrameworkUrlInterface; in top and Http $http, in construnction and $this->_http = $http;
                                          – Ansar Husain
                                          4 hours ago










                                        • sorry i forget to add $this->_http = $http; but even added this line there is no redirection to login page but error is gone so what s the problem can you please help me
                                          – Developper Magento
                                          4 hours ago










                                        • Check post now i have updated code using your code in my answer use as it is
                                          – Ansar Husain
                                          3 hours ago















                                        up vote
                                        1
                                        down vote













                                        Use below code



                                        <?php
                                        namespace VenorModuleControllerIndex;
                                        use MagentoFrameworkViewResultPageFactory;
                                        use MagentoFrameworkAppActionContext;
                                        use MagentoCustomerModelSession;
                                        use MagentoFrameworkAppResponseHttp;
                                        use MagentoFrameworkUrlInterface;

                                        class Index extends MagentoFrameworkAppActionAction

                                        public function __construct(

                                        MagentoFrameworkAppActionContext $context,
                                        MagentoFrameworkAppResponseRedirectInterface $redirect,
                                        MagentoFrameworkUrlInterface $url,
                                        MagentoFrameworkAppResourceConnection $resource,
                                        MagentoStoreModelStoreManagerInterface $storeManager,
                                        MagentoCustomerModelSession $customerSession,
                                        MagenticiansEvaluationModelEvaluationFactory $EvaluationFactory,
                                        MagenticiansEvaluationModelEvaluation $eval,
                                        MagenticiansEvaluationModelResourceModelEvaluationCollection $evaluationCollection,
                                        MagentoFrameworkControllerResultFactory $result,
                                        MagentoFrameworkViewResultPageFactory $pageFactory,
                                        MagentoFrameworkAppResponseHttp $http

                                        )

                                        parent::__construct(
                                        $context
                                        );
                                        $this->EvaluationFactory = $EvaluationFactory;
                                        $this->_customerSession = $customerSession;
                                        $this->_storeManager = $storeManager;
                                        $this->resultRedirect = $result;
                                        $this->_resource = $resource;
                                        $this->_evaluation =$eval;
                                        $this->_evaluationCollection = $evaluationCollection;
                                        $this->redirect = $redirect;
                                        $this->urlInterface = $url;
                                        $this->_pageFactory = $pageFactory;
                                        $this->_http = $http;




                                        public function execute()

                                        $login_url = $this->urlInterface->getUrl('customer/account/login');
                                        $this->_http->setRedirect($login_url, 301);
                                        $pageFactory = $this->_pageFactory->create();
                                        return $pageFactory;








                                        share|improve this answer






















                                        • <b>Fatal error</b>: Uncaught Error: Call to a member function setRedirect() on null in /var/www/html/sajjada/app/code/Magenticians/Evaluation/Controller/Index/Index.php:123
                                          – Developper Magento
                                          4 hours ago










                                        • did you do use MagentoFrameworkUrlInterface; in top and Http $http, in construnction and $this->_http = $http;
                                          – Ansar Husain
                                          4 hours ago










                                        • sorry i forget to add $this->_http = $http; but even added this line there is no redirection to login page but error is gone so what s the problem can you please help me
                                          – Developper Magento
                                          4 hours ago










                                        • Check post now i have updated code using your code in my answer use as it is
                                          – Ansar Husain
                                          3 hours ago













                                        up vote
                                        1
                                        down vote










                                        up vote
                                        1
                                        down vote









                                        Use below code



                                        <?php
                                        namespace VenorModuleControllerIndex;
                                        use MagentoFrameworkViewResultPageFactory;
                                        use MagentoFrameworkAppActionContext;
                                        use MagentoCustomerModelSession;
                                        use MagentoFrameworkAppResponseHttp;
                                        use MagentoFrameworkUrlInterface;

                                        class Index extends MagentoFrameworkAppActionAction

                                        public function __construct(

                                        MagentoFrameworkAppActionContext $context,
                                        MagentoFrameworkAppResponseRedirectInterface $redirect,
                                        MagentoFrameworkUrlInterface $url,
                                        MagentoFrameworkAppResourceConnection $resource,
                                        MagentoStoreModelStoreManagerInterface $storeManager,
                                        MagentoCustomerModelSession $customerSession,
                                        MagenticiansEvaluationModelEvaluationFactory $EvaluationFactory,
                                        MagenticiansEvaluationModelEvaluation $eval,
                                        MagenticiansEvaluationModelResourceModelEvaluationCollection $evaluationCollection,
                                        MagentoFrameworkControllerResultFactory $result,
                                        MagentoFrameworkViewResultPageFactory $pageFactory,
                                        MagentoFrameworkAppResponseHttp $http

                                        )

                                        parent::__construct(
                                        $context
                                        );
                                        $this->EvaluationFactory = $EvaluationFactory;
                                        $this->_customerSession = $customerSession;
                                        $this->_storeManager = $storeManager;
                                        $this->resultRedirect = $result;
                                        $this->_resource = $resource;
                                        $this->_evaluation =$eval;
                                        $this->_evaluationCollection = $evaluationCollection;
                                        $this->redirect = $redirect;
                                        $this->urlInterface = $url;
                                        $this->_pageFactory = $pageFactory;
                                        $this->_http = $http;




                                        public function execute()

                                        $login_url = $this->urlInterface->getUrl('customer/account/login');
                                        $this->_http->setRedirect($login_url, 301);
                                        $pageFactory = $this->_pageFactory->create();
                                        return $pageFactory;








                                        share|improve this answer














                                        Use below code



                                        <?php
                                        namespace VenorModuleControllerIndex;
                                        use MagentoFrameworkViewResultPageFactory;
                                        use MagentoFrameworkAppActionContext;
                                        use MagentoCustomerModelSession;
                                        use MagentoFrameworkAppResponseHttp;
                                        use MagentoFrameworkUrlInterface;

                                        class Index extends MagentoFrameworkAppActionAction

                                        public function __construct(

                                        MagentoFrameworkAppActionContext $context,
                                        MagentoFrameworkAppResponseRedirectInterface $redirect,
                                        MagentoFrameworkUrlInterface $url,
                                        MagentoFrameworkAppResourceConnection $resource,
                                        MagentoStoreModelStoreManagerInterface $storeManager,
                                        MagentoCustomerModelSession $customerSession,
                                        MagenticiansEvaluationModelEvaluationFactory $EvaluationFactory,
                                        MagenticiansEvaluationModelEvaluation $eval,
                                        MagenticiansEvaluationModelResourceModelEvaluationCollection $evaluationCollection,
                                        MagentoFrameworkControllerResultFactory $result,
                                        MagentoFrameworkViewResultPageFactory $pageFactory,
                                        MagentoFrameworkAppResponseHttp $http

                                        )

                                        parent::__construct(
                                        $context
                                        );
                                        $this->EvaluationFactory = $EvaluationFactory;
                                        $this->_customerSession = $customerSession;
                                        $this->_storeManager = $storeManager;
                                        $this->resultRedirect = $result;
                                        $this->_resource = $resource;
                                        $this->_evaluation =$eval;
                                        $this->_evaluationCollection = $evaluationCollection;
                                        $this->redirect = $redirect;
                                        $this->urlInterface = $url;
                                        $this->_pageFactory = $pageFactory;
                                        $this->_http = $http;




                                        public function execute()

                                        $login_url = $this->urlInterface->getUrl('customer/account/login');
                                        $this->_http->setRedirect($login_url, 301);
                                        $pageFactory = $this->_pageFactory->create();
                                        return $pageFactory;









                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited 3 hours ago

























                                        answered 4 hours ago









                                        Ansar Husain

                                        1,182216




                                        1,182216











                                        • <b>Fatal error</b>: Uncaught Error: Call to a member function setRedirect() on null in /var/www/html/sajjada/app/code/Magenticians/Evaluation/Controller/Index/Index.php:123
                                          – Developper Magento
                                          4 hours ago










                                        • did you do use MagentoFrameworkUrlInterface; in top and Http $http, in construnction and $this->_http = $http;
                                          – Ansar Husain
                                          4 hours ago










                                        • sorry i forget to add $this->_http = $http; but even added this line there is no redirection to login page but error is gone so what s the problem can you please help me
                                          – Developper Magento
                                          4 hours ago










                                        • Check post now i have updated code using your code in my answer use as it is
                                          – Ansar Husain
                                          3 hours ago

















                                        • <b>Fatal error</b>: Uncaught Error: Call to a member function setRedirect() on null in /var/www/html/sajjada/app/code/Magenticians/Evaluation/Controller/Index/Index.php:123
                                          – Developper Magento
                                          4 hours ago










                                        • did you do use MagentoFrameworkUrlInterface; in top and Http $http, in construnction and $this->_http = $http;
                                          – Ansar Husain
                                          4 hours ago










                                        • sorry i forget to add $this->_http = $http; but even added this line there is no redirection to login page but error is gone so what s the problem can you please help me
                                          – Developper Magento
                                          4 hours ago










                                        • Check post now i have updated code using your code in my answer use as it is
                                          – Ansar Husain
                                          3 hours ago
















                                        <b>Fatal error</b>: Uncaught Error: Call to a member function setRedirect() on null in /var/www/html/sajjada/app/code/Magenticians/Evaluation/Controller/Index/Index.php:123
                                        – Developper Magento
                                        4 hours ago




                                        <b>Fatal error</b>: Uncaught Error: Call to a member function setRedirect() on null in /var/www/html/sajjada/app/code/Magenticians/Evaluation/Controller/Index/Index.php:123
                                        – Developper Magento
                                        4 hours ago












                                        did you do use MagentoFrameworkUrlInterface; in top and Http $http, in construnction and $this->_http = $http;
                                        – Ansar Husain
                                        4 hours ago




                                        did you do use MagentoFrameworkUrlInterface; in top and Http $http, in construnction and $this->_http = $http;
                                        – Ansar Husain
                                        4 hours ago












                                        sorry i forget to add $this->_http = $http; but even added this line there is no redirection to login page but error is gone so what s the problem can you please help me
                                        – Developper Magento
                                        4 hours ago




                                        sorry i forget to add $this->_http = $http; but even added this line there is no redirection to login page but error is gone so what s the problem can you please help me
                                        – Developper Magento
                                        4 hours ago












                                        Check post now i have updated code using your code in my answer use as it is
                                        – Ansar Husain
                                        3 hours ago





                                        Check post now i have updated code using your code in my answer use as it is
                                        – Ansar Husain
                                        3 hours ago


















                                         

                                        draft saved


                                        draft discarded















































                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f242273%2fhow-to-redirect-customer-from-controller-not-observer-to-login-page-magento-2%23new-answer', 'question_page');

                                        );

                                        Post as a guest













































































                                        Comments

                                        Popular posts from this blog

                                        Long meetings (6-7 hours a day): Being “babysat” by supervisor

                                        Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                                        Confectionery