Magento2 | How to call Template from a Controller The Next CEO of Stack Overflowload 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 ButtonHow to override this block and template in my module?Magento 2.2.5: Overriding Admin Controller sales/order

Example of a Mathematician/Physicist whose Other Publications during their PhD eclipsed their PhD Thesis

Sending manuscript to multiple publishers

Why do professional authors make "consistency" mistakes? And how to avoid them?

Why am I allowed to create multiple unique pointers from a single object?

What's the best way to handle refactoring a big file?

Would a galaxy be visible from outside, but nearby?

Bold, vivid family

Rotate a column

Is it possible to search for a directory/file combination?

Are there any limitations on attacking while grappling?

What is the purpose of the Evocation wizard's Potent Cantrip feature?

How should I support this large drywall patch?

Why don't programming languages automatically manage the synchronous/asynchronous problem?

Non-deterministic sum of floats

Unreliable Magic - Is it worth it?

Interfacing a button to MCU (and PC) with 50m long cable

I believe this to be a fraud - hired, then asked to cash check and send cash as Bitcoin

Novel about a guy who is possessed by the divine essence and the world ends?

Anatomically Correct Strange Women In Ponds Distributing Swords

Received an invoice from my ex-employer billing me for training; how to handle?

Elegant way to replace substring in a regex with optional groups in Python?

FBX seems to be empty when imported into Blender

Why has the US not been more assertive in confronting Russia in recent years?

At which OSI layer a user-generated data resides?



Magento2 | How to call Template from a Controller



The Next CEO of Stack Overflowload 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 ButtonHow to override this block and template in my module?Magento 2.2.5: Overriding Admin Controller sales/order










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 1 min ago







k33n

















asked Jul 3 '18 at 11:51









k33nk33n

455110




455110







  • 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










1 Answer
1






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























    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

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    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

        925315




        925315



























            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

            Best approach to update all entries in a list that is paginated?Best way to add items to a paginated listChoose Your Country: Best Usability approachUpdate list when a user is viewing the list without annoying themWhen would the best day to update your webpage be?What should happen when I add a Row to a paginated, sorted listShould I adopt infinite scrolling or classical pagination?How to show user that page objects automatically updateWhat is the best location to locate the comments section in a list pageBest way to combine filtering and selecting items in a listWhen one of two inputs must be updated to satisfy a consistency criteria, which should you update (if at all)?

            Вунгтау (аеропорт) Загальні відомості | Див. також | Посилання | Навігаційне меню10°22′00″ пн. ш. 107°05′00″ сх. д. / 10.36667° пн. ш. 107.08333° сх. д. / 10.36667; 107.0833310°22′00″ пн. ш. 107°05′00″ сх. д. / 10.36667° пн. ш. 107.08333° сх. д. / 10.36667; 107.083337731608Vinh AirportVinh airport facelift improves serviceвиправивши або дописавши їївиправивши або дописавши їїр

            Тонконіг бульбистий Зміст Опис | Поширення | Екологія | Господарське значення | Примітки | Див. також | Література | Джерела | Посилання | Навігаційне меню1114601320038-241116202404kew-435458Poa bulbosaЭлектронный каталог сосудистых растений Азиатской России [Електронний каталог судинних рослин Азіатської Росії]Малышев Л. Л. Дикие родичи культурных растений. Poa bulbosa L. - Мятлик луковичный. [Малишев Л. Л. Дикі родичи культурних рослин. Poa bulbosa L. - Тонконіг бульбистий.]Мятлик (POA) Сем. Злаки (Мятликовые) [Тонконіг (POA) Род. Злаки (Тонконогові)]Poa bulbosa Linnaeus, Sp. Pl. 1: 70. 1753. 鳞茎早熟禾 lin jing zao shu he (Description from Flora of China) [Poa bulbosa Linnaeus, Sp. Pl. 1: 70. 1753. 鳞茎早熟禾 lin jing zao shu he (Опис від Флора Китаю)]Poa bulbosa L. – lipnice cibulkatá / lipnica cibulkatáPoa bulbosa в базі даних Poa bulbosa на сайті Poa bulbosa в базі даних «Global Biodiversity Information Facility» (GBIF)Poa bulbosa в базі даних «Euro + Med PlantBase» — інформаційному ресурсі для Євро-середземноморського розмаїття рослинPoa bulbosa L. на сайті «Плантариум»