Magento2 | How to call Template from a Controllerload magento controller method from TemplateHow to call a controller action from another module?How to call a model method from controller in Magento2Form Key Value in template and controllerMagento 2 - Got Error in Adminhtml Controller“Undefined property: Interceptor::$invoiceService” while overriding controller in magento2How to create Magento Order programatically in Amazon M2eproCall Controller from ButtonMagento 2.2.5: Overriding Admin Controller sales/orderMagento2: How can call observer function in controller file?

My employer is refusing to give me the pay that was advertised after an internal job move

Rampant sharing of authorship among colleagues in the name of "collaboration". Is not taking part in it a death knell for a future in academia?

Unknown indication below upper stave

Security measures that could plausibly last 150+ years?

How to choose using Collection<Id> rather than Collection<String>, or the opposite?

How can Paypal know my card is being used in another account?

Avoiding Implicit Conversion in Constructor. Explicit keyword doesn't help here

How to foreshadow to avoid a 'deus ex machina'-construction

What force enables us to walk? Friction or normal reaction?

Are all French verb conjugation tenses and moods practical and efficient?

Is it possible for a particle to decay via gravity?

Correct word for a little toy that always stands up?

What is the highest achievable score in Catan

Can you continue the movement of a Bonus Action Dash granted by Expeditious Retreat if your Concentration is broken mid-move?

What are the cons of stateless password generators?

Why did I lose on time with 3 pawns vs Knight. Shouldn't it be a draw?

Why did some Apollo missions carry a grenade launcher?

Why are subdominants unstable?

Boots or trail runners with reference to blisters?

What is my clock telling me to do?

Is Ear Protection Necessary For General Aviation Airplanes?

How to innovate in OR

Why does the Rust compiler not optimize code assuming that two mutable references cannot alias?

Scam? Checks via Email



Magento2 | How to call Template from a Controller


load magento controller method from TemplateHow to call a controller action from another module?How to call a model method from controller in Magento2Form Key Value in template and controllerMagento 2 - Got Error in Adminhtml Controller“Undefined property: Interceptor::$invoiceService” while overriding controller in magento2How to create Magento Order programatically in Amazon M2eproCall Controller from ButtonMagento 2.2.5: Overriding Admin Controller sales/orderMagento2: How can call observer function in controller file?






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








0















I got this controller:



<?php
namespace

clas extends MagentoSalesControllerAdminhtmlOrder

public function execute()


echo "TEST";


?>


I want it to call a template. How can I do that?










share|improve this question





















  • 1





    create handler on layout file and call phtml.

    – Sukumar Gorai
    Jul 3 '18 at 12:01











  • From a front-end dev - Please do not do it this way, calling a template from a PHP files makes life much more difficult if a FE dev ever wants to debug or make changes via layout XML.

    – Ben Crook
    Jul 3 '18 at 12:07

















0















I got this controller:



<?php
namespace

clas extends MagentoSalesControllerAdminhtmlOrder

public function execute()


echo "TEST";


?>


I want it to call a template. How can I do that?










share|improve this question





















  • 1





    create handler on layout file and call phtml.

    – Sukumar Gorai
    Jul 3 '18 at 12:01











  • From a front-end dev - Please do not do it this way, calling a template from a PHP files makes life much more difficult if a FE dev ever wants to debug or make changes via layout XML.

    – Ben Crook
    Jul 3 '18 at 12:07













0












0








0


1






I got this controller:



<?php
namespace

clas extends MagentoSalesControllerAdminhtmlOrder

public function execute()


echo "TEST";


?>


I want it to call a template. How can I do that?










share|improve this question
















I got this controller:



<?php
namespace

clas extends MagentoSalesControllerAdminhtmlOrder

public function execute()


echo "TEST";


?>


I want it to call a template. How can I do that?







magento2 template controllers






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 29 at 22:15







k33n

















asked Jul 3 '18 at 11:51









k33nk33n

4551 silver badge16 bronze badges




4551 silver badge16 bronze badges










  • 1





    create handler on layout file and call phtml.

    – Sukumar Gorai
    Jul 3 '18 at 12:01











  • From a front-end dev - Please do not do it this way, calling a template from a PHP files makes life much more difficult if a FE dev ever wants to debug or make changes via layout XML.

    – Ben Crook
    Jul 3 '18 at 12:07












  • 1





    create handler on layout file and call phtml.

    – Sukumar Gorai
    Jul 3 '18 at 12:01











  • From a front-end dev - Please do not do it this way, calling a template from a PHP files makes life much more difficult if a FE dev ever wants to debug or make changes via layout XML.

    – Ben Crook
    Jul 3 '18 at 12:07







1




1





create handler on layout file and call phtml.

– Sukumar Gorai
Jul 3 '18 at 12:01





create handler on layout file and call phtml.

– Sukumar Gorai
Jul 3 '18 at 12:01













From a front-end dev - Please do not do it this way, calling a template from a PHP files makes life much more difficult if a FE dev ever wants to debug or make changes via layout XML.

– Ben Crook
Jul 3 '18 at 12:07





From a front-end dev - Please do not do it this way, calling a template from a PHP files makes life much more difficult if a FE dev ever wants to debug or make changes via layout XML.

– Ben Crook
Jul 3 '18 at 12:07










2 Answers
2






active

oldest

votes


















3














check the below code



 /**
* @var MagentoFrameworkViewResultPageFactory
*/
protected $_resultPageFactory;

/**
* @param Context $context
* @param PageFactory $resultPageFactory
*/
public function __construct(
Context $context,
PageFactory $resultPageFactory

)
$this->_resultPageFactory = $resultPageFactory;
parent::__construct($context);



public function execute()


$resultPage = $this->_resultPageFactory->create();
$resultPage->getConfig()->getTitle()->prepend(__(' heading '));

$block = $resultPage->getLayout()
->createBlock('CompanynameModuleBlockblockname')
->setTemplate('Companyname_Module::test.phtml')
->toHtml();
$this->getResponse()->setBody($block);

}





share|improve this answer
































    0














    In controller:



    use MagentoFrameworkAppActionAction;
    use MagentoFrameworkControllerResultJsonFactory;
    use MagentoFrameworkViewResultPageFactory;
    use MagentoFrameworkAppActionContext;
    use OXOfferNotifierHelperData;

    class Index extends Action


    protected $_resultPageFactory;
    protected $_resultJsonFactory;
    protected $_helperData;

    public function __construct(Context $context, PageFactory $resultPageFactory, JsonFactory $resultJsonFactory, Data $data)

    parent::__construct($context);
    $this->_resultPageFactory = $resultPageFactory;
    $this->_resultJsonFactory = $resultJsonFactory;
    $this->_helperData = $data;


    public function execute()

    if ($this->_helperData->isOfferNotifierEnabled())
    $result = $this->_resultJsonFactory->create();
    $resultPage = $this->_resultPageFactory->create();
    $productId = $this->getRequest()->getParam("productId");
    $offerInProductView = $this->getRequest()->getParam("offerInProductView") ?? 0;
    $url = $this->getRequest()->getParam("url") ?? 0;
    //Get the product id for ProductView Page
    if ($offerInProductView)
    $productInfo = $resultPage->getLayout()
    ->createBlock('OOOYYYBlockProductView')
    ->setData('product_id', $productId)
    ->setData('offer_in_product_view', $offerInProductView)
    ->setTemplate('OOO_YYY::product/view/offer-notifier.phtml')
    ->toHtml();



    $result->setData(['content' => $productInfo]);
    return $result;









    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%2f232177%2fmagento2-how-to-call-template-from-a-controller%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









      3














      check the below code



       /**
      * @var MagentoFrameworkViewResultPageFactory
      */
      protected $_resultPageFactory;

      /**
      * @param Context $context
      * @param PageFactory $resultPageFactory
      */
      public function __construct(
      Context $context,
      PageFactory $resultPageFactory

      )
      $this->_resultPageFactory = $resultPageFactory;
      parent::__construct($context);



      public function execute()


      $resultPage = $this->_resultPageFactory->create();
      $resultPage->getConfig()->getTitle()->prepend(__(' heading '));

      $block = $resultPage->getLayout()
      ->createBlock('CompanynameModuleBlockblockname')
      ->setTemplate('Companyname_Module::test.phtml')
      ->toHtml();
      $this->getResponse()->setBody($block);

      }





      share|improve this answer





























        3














        check the below code



         /**
        * @var MagentoFrameworkViewResultPageFactory
        */
        protected $_resultPageFactory;

        /**
        * @param Context $context
        * @param PageFactory $resultPageFactory
        */
        public function __construct(
        Context $context,
        PageFactory $resultPageFactory

        )
        $this->_resultPageFactory = $resultPageFactory;
        parent::__construct($context);



        public function execute()


        $resultPage = $this->_resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->prepend(__(' heading '));

        $block = $resultPage->getLayout()
        ->createBlock('CompanynameModuleBlockblockname')
        ->setTemplate('Companyname_Module::test.phtml')
        ->toHtml();
        $this->getResponse()->setBody($block);

        }





        share|improve this answer



























          3












          3








          3







          check the below code



           /**
          * @var MagentoFrameworkViewResultPageFactory
          */
          protected $_resultPageFactory;

          /**
          * @param Context $context
          * @param PageFactory $resultPageFactory
          */
          public function __construct(
          Context $context,
          PageFactory $resultPageFactory

          )
          $this->_resultPageFactory = $resultPageFactory;
          parent::__construct($context);



          public function execute()


          $resultPage = $this->_resultPageFactory->create();
          $resultPage->getConfig()->getTitle()->prepend(__(' heading '));

          $block = $resultPage->getLayout()
          ->createBlock('CompanynameModuleBlockblockname')
          ->setTemplate('Companyname_Module::test.phtml')
          ->toHtml();
          $this->getResponse()->setBody($block);

          }





          share|improve this answer













          check the below code



           /**
          * @var MagentoFrameworkViewResultPageFactory
          */
          protected $_resultPageFactory;

          /**
          * @param Context $context
          * @param PageFactory $resultPageFactory
          */
          public function __construct(
          Context $context,
          PageFactory $resultPageFactory

          )
          $this->_resultPageFactory = $resultPageFactory;
          parent::__construct($context);



          public function execute()


          $resultPage = $this->_resultPageFactory->create();
          $resultPage->getConfig()->getTitle()->prepend(__(' heading '));

          $block = $resultPage->getLayout()
          ->createBlock('CompanynameModuleBlockblockname')
          ->setTemplate('Companyname_Module::test.phtml')
          ->toHtml();
          $this->getResponse()->setBody($block);

          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jul 3 '18 at 12:07









          Prashant PatelPrashant Patel

          9513 silver badges15 bronze badges




          9513 silver badges15 bronze badges


























              0














              In controller:



              use MagentoFrameworkAppActionAction;
              use MagentoFrameworkControllerResultJsonFactory;
              use MagentoFrameworkViewResultPageFactory;
              use MagentoFrameworkAppActionContext;
              use OXOfferNotifierHelperData;

              class Index extends Action


              protected $_resultPageFactory;
              protected $_resultJsonFactory;
              protected $_helperData;

              public function __construct(Context $context, PageFactory $resultPageFactory, JsonFactory $resultJsonFactory, Data $data)

              parent::__construct($context);
              $this->_resultPageFactory = $resultPageFactory;
              $this->_resultJsonFactory = $resultJsonFactory;
              $this->_helperData = $data;


              public function execute()

              if ($this->_helperData->isOfferNotifierEnabled())
              $result = $this->_resultJsonFactory->create();
              $resultPage = $this->_resultPageFactory->create();
              $productId = $this->getRequest()->getParam("productId");
              $offerInProductView = $this->getRequest()->getParam("offerInProductView") ?? 0;
              $url = $this->getRequest()->getParam("url") ?? 0;
              //Get the product id for ProductView Page
              if ($offerInProductView)
              $productInfo = $resultPage->getLayout()
              ->createBlock('OOOYYYBlockProductView')
              ->setData('product_id', $productId)
              ->setData('offer_in_product_view', $offerInProductView)
              ->setTemplate('OOO_YYY::product/view/offer-notifier.phtml')
              ->toHtml();



              $result->setData(['content' => $productInfo]);
              return $result;









              share|improve this answer





























                0














                In controller:



                use MagentoFrameworkAppActionAction;
                use MagentoFrameworkControllerResultJsonFactory;
                use MagentoFrameworkViewResultPageFactory;
                use MagentoFrameworkAppActionContext;
                use OXOfferNotifierHelperData;

                class Index extends Action


                protected $_resultPageFactory;
                protected $_resultJsonFactory;
                protected $_helperData;

                public function __construct(Context $context, PageFactory $resultPageFactory, JsonFactory $resultJsonFactory, Data $data)

                parent::__construct($context);
                $this->_resultPageFactory = $resultPageFactory;
                $this->_resultJsonFactory = $resultJsonFactory;
                $this->_helperData = $data;


                public function execute()

                if ($this->_helperData->isOfferNotifierEnabled())
                $result = $this->_resultJsonFactory->create();
                $resultPage = $this->_resultPageFactory->create();
                $productId = $this->getRequest()->getParam("productId");
                $offerInProductView = $this->getRequest()->getParam("offerInProductView") ?? 0;
                $url = $this->getRequest()->getParam("url") ?? 0;
                //Get the product id for ProductView Page
                if ($offerInProductView)
                $productInfo = $resultPage->getLayout()
                ->createBlock('OOOYYYBlockProductView')
                ->setData('product_id', $productId)
                ->setData('offer_in_product_view', $offerInProductView)
                ->setTemplate('OOO_YYY::product/view/offer-notifier.phtml')
                ->toHtml();



                $result->setData(['content' => $productInfo]);
                return $result;









                share|improve this answer



























                  0












                  0








                  0







                  In controller:



                  use MagentoFrameworkAppActionAction;
                  use MagentoFrameworkControllerResultJsonFactory;
                  use MagentoFrameworkViewResultPageFactory;
                  use MagentoFrameworkAppActionContext;
                  use OXOfferNotifierHelperData;

                  class Index extends Action


                  protected $_resultPageFactory;
                  protected $_resultJsonFactory;
                  protected $_helperData;

                  public function __construct(Context $context, PageFactory $resultPageFactory, JsonFactory $resultJsonFactory, Data $data)

                  parent::__construct($context);
                  $this->_resultPageFactory = $resultPageFactory;
                  $this->_resultJsonFactory = $resultJsonFactory;
                  $this->_helperData = $data;


                  public function execute()

                  if ($this->_helperData->isOfferNotifierEnabled())
                  $result = $this->_resultJsonFactory->create();
                  $resultPage = $this->_resultPageFactory->create();
                  $productId = $this->getRequest()->getParam("productId");
                  $offerInProductView = $this->getRequest()->getParam("offerInProductView") ?? 0;
                  $url = $this->getRequest()->getParam("url") ?? 0;
                  //Get the product id for ProductView Page
                  if ($offerInProductView)
                  $productInfo = $resultPage->getLayout()
                  ->createBlock('OOOYYYBlockProductView')
                  ->setData('product_id', $productId)
                  ->setData('offer_in_product_view', $offerInProductView)
                  ->setTemplate('OOO_YYY::product/view/offer-notifier.phtml')
                  ->toHtml();



                  $result->setData(['content' => $productInfo]);
                  return $result;









                  share|improve this answer













                  In controller:



                  use MagentoFrameworkAppActionAction;
                  use MagentoFrameworkControllerResultJsonFactory;
                  use MagentoFrameworkViewResultPageFactory;
                  use MagentoFrameworkAppActionContext;
                  use OXOfferNotifierHelperData;

                  class Index extends Action


                  protected $_resultPageFactory;
                  protected $_resultJsonFactory;
                  protected $_helperData;

                  public function __construct(Context $context, PageFactory $resultPageFactory, JsonFactory $resultJsonFactory, Data $data)

                  parent::__construct($context);
                  $this->_resultPageFactory = $resultPageFactory;
                  $this->_resultJsonFactory = $resultJsonFactory;
                  $this->_helperData = $data;


                  public function execute()

                  if ($this->_helperData->isOfferNotifierEnabled())
                  $result = $this->_resultJsonFactory->create();
                  $resultPage = $this->_resultPageFactory->create();
                  $productId = $this->getRequest()->getParam("productId");
                  $offerInProductView = $this->getRequest()->getParam("offerInProductView") ?? 0;
                  $url = $this->getRequest()->getParam("url") ?? 0;
                  //Get the product id for ProductView Page
                  if ($offerInProductView)
                  $productInfo = $resultPage->getLayout()
                  ->createBlock('OOOYYYBlockProductView')
                  ->setData('product_id', $productId)
                  ->setData('offer_in_product_view', $offerInProductView)
                  ->setTemplate('OOO_YYY::product/view/offer-notifier.phtml')
                  ->toHtml();



                  $result->setData(['content' => $productInfo]);
                  return $result;










                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jul 21 at 14:38









                  Mano MMano M

                  1,1804 silver badges25 bronze badges




                  1,1804 silver badges25 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%2f232177%2fmagento2-how-to-call-template-from-a-controller%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

                      Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

                      Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

                      Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?