Quantcast
Channel: Magento – Ken Nguyen
Viewing all articles
Browse latest Browse all 14

[Magento 2] Create new controller

$
0
0
  1. Create your directory under <magento_root>/app/code/Mydogs/Helloworld/
  2. Create etc/module.xml file under Helloworld directory and define your module.
    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">

        <module name="Mydogs_Helloworld" schema_version="0.0.1" setup_version="0.0.1" />

    </config>
    
  3. Create etc/frontend/routes.xml and declare your router for Helloworld module.
    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
        <router id="standard">
            <route id="helloworld" frontName="helloworld">
                <module name="Mydogs_Helloworld"/>
            </route>
        </router>
    </config>
  4. Create Controller/Index/Index.php and write below code in it.
    <?php
    namespace Mydogs\Helloworld\Controller\Index;
    use Magento\Framework\Controller\ResultFactory;
    use Magento\Framework\View\Result\PageFactory;
    
    class Index extends \Magento\Framework\App\Action\Action
    {
            /**
             * @var \Magento\Framework\View\Result\PageFactory
             */
            protected $resultPageFactory;
    
            /**
             * @param \Magento\Framework\App\Action\Context $context
             * @param \Magento\Framework\View\Result\PageFactory resultPageFactory
             */
            public function __construct(
                \Magento\Framework\App\Action\Context $context,
                PageFactory $resultPageFactory
            )
            {
                $this->resultPageFactory = $resultPageFactory;
                parent::__construct($context);
            }
        /**
         * Default customer account page
         *
         * @return void
         */
        public function execute()
        {
            $params = $this->getRequest()->getParams();
    
            $responseContent = array();
    
            if(isset($params['name'])){
                $name = $params['name'];
            }
        
            $this->getResponse()->representJson(
                $this->_objectManager->get('Magento\Framework\Json\Helper\Data')->jsonEncode($responseContent)
            );
        }
        
    }
    ?>
  5. Create your layout and template files under view/frontend/layout/helloworld_index_index.xml
    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
        <body>
        	<referenceContainer name="content">
                <block class="Magento\Framework\View\Element\Template" name="hello.world" as="helloWorld" template="Mydogs_Helloworld::hello-how-are-you.phtml" before="-" />
            </referenceContainer>
        </body>
    </page>

    and view/frontend/templates/hello-how-are-you.phtml respectively.

    <h1>Hello World</h1>
    <p>this is hello-how-are-you.phtml</p>
  6. Open /app/etc/config.php and define your module in an array.
  7. Run command line:  php bin/magento setup:upgrade

Viewing all articles
Browse latest Browse all 14

Trending Articles