1

I was confusing among extensions and modules.

Can anyone guide me about it and i am looking for good tutorial for creating extensions in magento 2.1.6. ?

Muhammad Anas
  • 1,467
  • 3
  • 12
  • 33
Funti
  • 11
  • 1
  • 3

1 Answers1

3

Here are step to create the Magento2 Extension

create registration.php

app/code/QaisarSatti/HelloWorld/registration.php

<?php
/**
* Simple Hello World Module
*
* @category QaisarSatti
* @package QaisarSatti_HelloWorld
* @author Muhammad Qaisar Satti
* @Email qaisarssatti@gmail.com
*
*/
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'QaisarSatti_HelloWorld',
__DIR__
);

Create module.xml

app/code/QaisarSatti/HelloWorld/etc/module.xml

<?xml version="1.0"?>
<!--/**
* Simple Hello World Module
*
* @category QaisarSatti
* @package QaisarSatti_HelloWorld
* @author Muhammad Qaisar Satti
* @Email qaisarssatti@gmail.com
*
*/ -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="QaisarSatti_HelloWorld" schema_version="0.0.1" setup_version="0.0.1"/>
</config>

Create block

app/code/QaisarSatti/HelloWorld/Block/HelloWorld.php

<?php
/**
* Simple Hello World Module
*
* @category QaisarSatti
* @package QaisarSatti_HelloWorld
* @author Muhammad Qaisar Satti
* @Email qaisarssatti@gmail.com
*
*/
namespace QaisarSatti\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
public function _prepareLayout()
{
parent::_prepareLayout();
$this->pageConfig->getTitle()->set(__('First Hello World Module'));
return $this;
}
}

Create controller

app/code/QaisarSatti/HelloWorld/Controller/Index/Index.php

<?php
/**
* Simple Hello World Module
*
* @category QaisarSatti
* @package QaisarSatti_HelloWorld
* @author Muhammad Qaisar Satti
* @Email qaisarssatti@gmail.com
*
*/
namespace QaisarSatti\HelloWorld\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
public function execute()
{
$this->_view->loadLayout();
$this->_view->renderLayout();
}
}

Create frontend router

app/code/QaisarSatti/HelloWorld/etc/frontend/routes.xml

<?xml version="1.0"?>
<!--/**
* Simple Hello World Module
*
* @category QaisarSatti
* @package QaisarSatti_HelloWorld
* @author Muhammad Qaisar Satti
* @Email qaisarssatti@gmail.com
*
*/ -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="helloworld" frontName="helloworld">
<module name="QaisarSatti_HelloWorld" />
</route>
</router>
</config>

Create frontend template

app/code/QaisarSatti/HelloWorld/view/frontend/templates/HelloWorld.phtml

<?php
/**
* Catalog Product Rewrite Helper
*
* @category QaisarSatti
* @package QaisarSatti_HelloWorld
* @author Muhammad Qaisar Satti
* @Email qaisarssatti@gmail.com
*
*/
echo 'Hello World';
?>

Add frontend layout handle

app/code/QaisarSatti/HelloWorld/view/frontend/layout/helloworld_index_index.xml

<?xml version="1.0"?>
<!--/**
* Simple Hello World Module
*
* @category QaisarSatti
* @package QaisarSatti_HelloWorld
* @author Muhammad Qaisar Satti
* @Email qaisarssatti@gmail.com
*
*/ -->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="QaisarSatti\HelloWorld\Block\HelloWorld" name="HelloWorld" template="QaisarSatti_HelloWorld::HelloWorld.phtml"></block>
</referenceContainer>
</body>
</page>

You hello world module is complete .

Now run php bin/magento setup:upgrade command

Complete code on github

Tutorial

Qaisar Satti
  • 32,469
  • 18
  • 85
  • 137
  • thankyou Qaisar So can i assume that module and extensions are same ? right ? – Funti May 09 '17 at 12:44
  • @Funti read this for better understanding https://magento.stackexchange.com/questions/69849/what-is-the-difference-between-an-extension-and-a-module/69850#69850 – Qaisar Satti May 09 '17 at 12:45
  • i followed the above example but i am getting these issues

    RuntimeException: Can't create directory /var/www/html/magento2.0/var/generation/Magento/Framework/App/Response/Http/. in /var/www/html/magento2.0/vendor/magento/framework/Code/Generator.php:115 Stack trace: #0 /var/www/html/magento2.0/vendor/magento/framework/Code/Generator/Autoloader.php(35):

    – Funti May 09 '17 at 19:11
  • @Funti this is permission error kindly give 777 permission to folders. – Qaisar Satti May 10 '17 at 04:35
  • Mentiond error are removed. Should i access my created module like this [ROOT]/helloworld ?

    I tried , it says 404 Not found page

    – Funti May 10 '17 at 05:18
  • @Funti did you run php bin/magento setup:upgrade and module is shown in list? – Qaisar Satti May 10 '17 at 05:25
  • just run the upgrade command and all is blank now and getting 500 internal error in the console – Funti May 10 '17 at 06:37
  • @Funti can you share the error? – Qaisar Satti May 10 '17 at 07:35
  • Now i have resolved all errors all are fine expect the module accessing magento project is accessing fine ,but when i access the root/helloworld its only a blank page – Funti May 10 '17 at 07:39
  • your email please ? – Funti May 10 '17 at 07:59
  • @Funti you can find it here https://magento.stackexchange.com/users/24541/qaisar-satti?tab=profile – Qaisar Satti May 10 '17 at 08:57