{"id":2855,"date":"2016-04-29T21:56:41","date_gmt":"2016-04-29T16:26:41","guid":{"rendered":"http:\/\/www.magewallet.com\/?p=221"},"modified":"2020-03-13T16:08:12","modified_gmt":"2020-03-13T10:38:12","slug":"create-magento-2-extension","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento-2\/create-magento-2-extension\/","title":{"rendered":"How to create a Magento 2 extension"},"content":{"rendered":"<p>Happy to see you guys again!<\/p>\n<p>In this blog post, we will learn how to create Magento 2 extension step by step and basic knowledge required to setup a module. we will also introduce the coding of Magento 2 in the form of a &#8220;Hello Developer&#8221; style. This blog post also includes some basic functionality, covering as many development aspects as possible in this case.<\/p>\n<p>Lets consider module with Namespace is <strong>Hiddentechies<\/strong> and Module Name is <strong>HelloDeveloper<\/strong>. Lets list down all the files require for this extension.<\/p>\n<p>&#8211; app\/code\/Hiddentechies\/HelloDeveloper\/registration.php<br \/>\n&#8211; app\/code\/Hiddentechies\/HelloDeveloper\/composer.json<br \/>\n&#8211; app\/code\/Hiddentechies\/HelloDeveloper\/etc\/module.xml<br \/>\n&#8211; app\/code\/Hiddentechies\/HelloDeveloper\/etc\/frontend\/routes.xml<br \/>\n&#8211; app\/code\/Hiddentechies\/HelloDeveloper\/Controller\/Index\/index.php<br \/>\n&#8211; app\/code\/Hiddentechies\/Helloworld\/View\/frontend\/layout\/hellodeveloper_index_index.xml<br \/>\n&#8211; app\/code\/Hiddentechies\/HelloDeveloper\/Block\/HelloDeveloper.php<br \/>\n&#8211; app\/code\/Hiddentechies\/HelloDeveloper\/View\/frontend\/templates\/hellodeveloper.phtml<\/p>\n<p>First we will create <strong>registration<\/strong> file at app\/code\/Hiddentechies\/HelloDeveloper\/registration.php file<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n\\Magento\\Framework\\Component\\ComponentRegistrar::register(\r\n    \\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,\r\n    'Hiddentechies_HelloDeveloper',\r\n    __DIR__\r\n);<\/pre>\n<p>Now, create a <strong>composer.json<\/strong> file in app\/code\/Hiddentechies\/HelloDeveloper\/ folder as per below.<\/p>\n<pre class=\"lang:default decode:true \">{\r\n    \"name\": \"Hiddentechies\/hellodeveloper\",\r\n    \"description\": \"N\/A\",\r\n    \"require\": {\r\n        \"php\": \"~5.5.0|~5.6.0|~7.0.0\"\r\n    },\r\n    \"type\": \"magento2-module\",\r\n    \"version\": \"1.0.2\",\r\n    \"authors\": [\r\n        {\r\n            \"name\": \"Hiddentechies\",\r\n            \"email\": \"admin@Hiddentechies.com\",\r\n            \"homepage\": \"https:\/\/www.Hiddentechies.com\/\",\r\n            \"role\": \"Developer\"\r\n        }\r\n    ],\r\n    \"autoload\": {\r\n        \"files\": [\r\n            \"registration.php\"\r\n        ],\r\n        \"psr-4\": {\r\n            \"Hiddentechies\\\\HelloDeveloper\\\\\": \"\"\r\n        }\r\n    }\r\n}<\/pre>\n<p>now create <strong>module.xml<\/strong> file in app\/code\/Hiddentechies\/HelloDeveloper\/etc<\/p>\n<pre class=\"lang:default decode:true \">&lt;?xml version=\"1.0\"?&gt;\r\n \r\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"..\/..\/..\/..\/..\/lib\/internal\/Magento\/Framework\/Module\/etc\/module.xsd\"&gt;\r\n    &lt;module name=\"Hiddentechies_HelloDeveloper\" setup_version=\"1.0.0\"&gt;\r\n    &lt;\/module&gt;\r\n&lt;\/config&gt;\r\n<\/pre>\n<p>Create a frontend <strong>router<\/strong> in app\/code\/Hiddentechies\/HelloDeveloper\/etc\/frontend\/routes.xml<\/p>\n<pre class=\"lang:default decode:true \">&lt;?xml version=\"1.0\"?&gt;\r\n \r\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"..\/..\/..\/..\/..\/..\/lib\/internal\/Magento\/Framework\/App\/etc\/routes.xsd\"&gt;\r\n    &lt;router id=\"standard\"&gt;\r\n        &lt;route id=\"hellodeveloper\" frontName=\"hellodeveloper\"&gt;\r\n            &lt;module name=\"Hiddentechies_HelloDeveloper\" \/&gt;\r\n        &lt;\/route&gt;\r\n    &lt;\/router&gt;\r\n&lt;\/config&gt;<\/pre>\n<p>here the router ID shown which router we will use either frontend or adminhtml. Pay attention that the front name is the first part of the URL and it should be unique.<\/p>\n<p>Create a <strong>Controller<\/strong> action<\/p>\n<p>Create the file index.php in app\/code\/Hiddentechies\/HelloDeveloper\/Controller\/Index.<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php \r\nnamespace Hiddentechies\\HelloDeveloper\\Controller\\Index; \r\n\r\nclass Index extends \\Magento\\Framework\\App\\Action\\Action {\r\n    \/** @var  \\Magento\\Framework\\View\\Result\\Page *\/\r\n    protected $resultPageFactory;\r\n    \/**      * @param \\Magento\\Framework\\App\\Action\\Context $context      *\/\r\n    public function __construct(\\Magento\\Framework\\App\\Action\\Context $context,\r\n        \\Magento\\Framework\\View\\Result\\PageFactory $resultPageFactory)     {\r\n        $this-&gt;resultPageFactory = $resultPageFactory;\r\n        parent::__construct($context);\r\n    }\r\n\r\n    \/**\r\n     * Blog Index, shows a list of recent blog posts.\r\n     *\r\n     * @return \\Magento\\Framework\\View\\Result\\PageFactory\r\n     *\/\r\n    public function execute()\r\n    {\r\n    \t$resultPage = $this-&gt;resultPageFactory-&gt;create();\r\n    \t$resultPage-&gt;getConfig()-&gt;getTitle()-&gt;prepend(__('Hiddentechies HelloDeveloper'));\r\n    \treturn $resultPage;\r\n    }\r\n}<\/pre>\n<p>Create a <strong>layout<\/strong> file at app\\code\\Hiddentechies\\Helloworld\\View\\frontend\\layout\\hellodeveloper_index_index.xml<\/p>\n<pre class=\"lang:default decode:true \">&lt;?xml version=\"1.0\"?&gt;\r\n&lt;page xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" layout=\"1column\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd\"&gt;\r\n\t&lt;body&gt;\r\n\t\t&lt;referenceContainer name=\"content\"&gt;\r\n\t\t\t&lt;block class=\"Hiddentechies\\HelloDeveloper\\Block\\HelloDeveloper\" name=\"hiddentechies_hellodeveloper\" template=\"hellodeveloper.phtml\"&gt;\r\n\t\t\t&lt;\/block&gt;\r\n\t\t&lt;\/referenceContainer&gt;\r\n\t&lt;\/body&gt;\r\n&lt;\/page&gt;\r\n<\/pre>\n<p>Create <strong>block<\/strong> file app\/code\/Hiddentechies\/HelloDeveloper\/Block\/HelloDeveloper.php<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\nnamespace Hiddentechies\\HelloDeveloper\\Block;\r\n\r\nclass HelloDeveloper extends \\Magento\\Framework\\View\\Element\\Template\r\n{\r\n\tpublic function _prepareLayout()\r\n\t{\r\n\t\treturn parent::_prepareLayout();\r\n\t}\r\n}\r\n<\/pre>\n<p>Create a <strong>template<\/strong> file app\/code\/Hiddentechies\/HelloDeveloper\/View\/frontend\/templates\/hellodeveloper.phtml<\/p>\n<pre class=\"lang:default decode:true \">&lt;h1 style=\"color:#f1703d\"&gt; Welcome to Hiddentechies Blog &lt;\/h2&gt;<\/pre>\n<p>Active Hiddentechies_HelloDeveloper extension<\/p>\n<p>Open Command line in folder root of Magento and run commands<\/p>\n<pre class=\"lang:default decode:true \">php bin\/magento setup:upgrade<\/pre>\n<p>In order to cross check the routing and controller access below URL.<br \/>\n<strong>http:\/\/localhost\/magento2\/hellodeveloper\/index\/index<\/strong><\/p>\n<p>You have known all the steps to write a simple extension in Magento 2. if you have any questions about this steps of Magento 2 extension development, please ask them in comments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Happy to see you guys again! In this blog post, we will learn how to create Magento 2 extension step by step and basic knowledge required to setup a module. we will also introduce the coding of Magento 2 in the form of a &#8220;Hello Developer&#8221; style. This blog post also includes some basic functionality,&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-2\/create-magento-2-extension\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":4884,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[203,10,38,204,205],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/2855"}],"collection":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=2855"}],"version-history":[{"count":3,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/2855\/revisions"}],"predecessor-version":[{"id":10508,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/2855\/revisions\/10508"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/4884"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=2855"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=2855"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=2855"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}