{"id":5379,"date":"2017-04-25T15:45:34","date_gmt":"2017-04-25T10:15:34","guid":{"rendered":"https:\/\/weblizar.com\/?p=5379"},"modified":"2025-03-26T16:24:43","modified_gmt":"2025-03-26T10:54:43","slug":"create-an-extension-in-magento-2","status":"publish","type":"post","link":"https:\/\/weblizar.com\/blog\/create-an-extension-in-magento-2\/","title":{"rendered":"How To Create A Hello World Extension In Magento 2"},"content":{"rendered":"<p>We are going to build a very simple extension in Magento 2. When finished, the extension &#8216;s output will say \u00a0&#8221; Hello Weblizar, This is your first hello world Magento Extension! &#8221; \u00a0in the block content on a custom frontend route.\u00a0Let us learn how to create an extension in magento 2 with hello world as our output.<\/p>\n<p><strong>Requirement :<\/strong><\/p>\n<p>At list, you have\u00a0latest Magento 2 version which is currently 2.1. Is installed on your local system.<\/p>\n<p>Before we start a Magento 2 extension development, there are two things people often forget and we recommend you to do:<\/p>\n<p><strong>1. Disable Magento cache\u00a0<\/strong><\/p>\n<p>Disabling Magento cache during development will save you some time because you won&#8217;t need to manually flush the cache every time you make changes to your code.<\/p>\n<p>The easiest way to disable cache is to go to Admin =&gt; System =&gt; Cache Management =&gt; select all cache types and disable them.<\/p>\n<p><strong>2. Put Magento into a developer mode<\/strong><\/p>\n<p>You should put Magento into a developer mode to ensure that you see all the errors Magento is throwing at you.<\/p>\n<p>In order to do this, open your terminal and go to the Magento 2 root. From there you should run the following command:<\/p>\n<blockquote><p>php bin\/magento deploy:mode:set developer<\/p><\/blockquote>\n<h2>How To Create An Extension In Magento:<\/h2>\n<p><strong>Creating the extension \u00a0files and folders:<\/strong><br \/>\n<strong>Extension \u00a0Setup<\/strong><br \/>\nThe first step is to create the extension folder and necessary files required to register a Magento extension.<br \/>\n1. Create the following folders:<\/p>\n<ul>\n<li>app\/code\/Weblizar<\/li>\n<li>app\/code\/Weblizar\/Helloworld<\/li>\n<\/ul>\n<p>The Weblizar folder is the extension namespace, and Helloworld is the extension\u00a0<strong>name<\/strong>.<br \/>\n<strong>Note:<\/strong> If you don&#8217;t have the code folder in your app directory, create it manually.<\/p>\n<p>2. Now that we have an extension folder, we need to create a module.XML file in the app\/code\/Weblizar\/Helloworld\/etc folder with the following code:<\/p>\n<blockquote><p>&lt;?xml version=&#8221;1.0&#8243;?&gt;<br \/>\n&lt;config xmlns:xsi=&#8221;http:\/\/www.w3.org\/2001\/XMLSchema-instance&#8221; xsi:noNamespaceSchemaLocation=&#8221;urn:magento:framework:Module\/etc\/module.xsd&#8221;&gt;<br \/>\n&lt;module name=&#8221;Weblizar_Helloworld&#8221; setup_version=&#8221;1.0.0&#8243;&gt;&lt;\/module&gt;<br \/>\n&lt;\/config&gt;<\/p><\/blockquote>\n<p>3. To register the module, create a registration.php file in the app\/code\/Weblizar\/Helloworld folder with the following code:<\/p>\n<blockquote><p>&lt;?php<br \/>\n\\Magento\\Framework\\Component\\ComponentRegistrar::register(<br \/>\n\\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,<br \/>\n&#8216;Weblizar_Helloworld&#8217;,<br \/>\n__DIR__<br \/>\n); \u00a0?&gt;<\/p><\/blockquote>\n<p>4. Open your terminal and go to the Magento 2 root. Run from there the following command:<\/p>\n<blockquote><p>php bin\/magento setup:upgrade<\/p><\/blockquote>\n<p>If you want to make sure that the extension \u00a0 is installed, you can go to Admin =&gt; Stores \u00a0=&gt; \u00a0Configuration =&gt; Advanced \u00a0=&gt; Advanced and check that the extension \u00a0 is present in the list or you can open app\/etc\/config.php and check the array for the &#8216;Weblizar_Helloworld&#8217; \u00a0key, \u00a0whose value should be set to 1.<\/p>\n<p><a href=\"https:\/\/weblizar.com\/blog\/wp-content\/uploads\/2017\/04\/magento-extation-admin.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5412\" src=\"https:\/\/weblizar.com\/blog\/wp-content\/uploads\/2017\/04\/magento-extation-admin.jpg\" alt=\"magento admin panel while creating an extension\" width=\"1916\" height=\"1008\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Creating a controller<\/strong><\/p>\n<p>1. First we need to define the router. To do this, create a routes.XML file in the app\/code\/Weblizar\/Helloworld\/etc\/frontend folder with the following code:<\/p>\n<blockquote><p>&lt;?xml version=&#8221;1.0&#8243;?&gt;<br \/>\n&lt;config xmlns:xsi=&#8221;http:\/\/www.w3.org\/2001\/XMLSchema-instance&#8221; xsi:noNamespaceSchemaLocation=&#8221;urn:magento:framework:App\/etc\/routes.xsd&#8221;&gt;<br \/>\n&lt;router id=&#8221;standard&#8221;&gt;<br \/>\n&lt;route id=&#8221;helloworld&#8221; frontName=&#8221;helloworld&#8221;&gt;<br \/>\n&lt;module name=&#8221;Weblizar_Helloworld&#8221; \/&gt;<br \/>\n&lt;\/route&gt;<br \/>\n&lt;\/router&gt;<br \/>\n&lt;\/config&gt;<\/p><\/blockquote>\n<p>Here we are defining our frontend router and route with an id \u00a0&#8221; helloworld &#8220;.<\/p>\n<p>The frontName attribute is going to be the first part of our URL.<\/p>\n<p>In Magento 2 URL\u2019s are constructed this way:<br \/>\n&lt;frontName&gt;\/&lt;controler_folder_name&gt;\/&lt;controller_class_name&gt;<\/p>\n<p>So in our example, the final URL will look like this:<\/p>\n<p>helloworld\/index\/index<\/p>\n<p>2. Now we create the Index.php controller file in the app\/code\/Weblizar\/Helloworld\/Controller\/Index folder with the following code:<\/p>\n<blockquote><p>&lt;?php<\/p>\n<p>namespace Weblizar\\Helloworld\\Controller\\Index;<\/p>\n<p>use Magento\\Framework\\App\\Action\\Context;<\/p>\n<p>class Index extends \\Magento\\Framework\\App\\Action\\Action<br \/>\n{<br \/>\nprotected $_resultPageFactory;<\/p>\n<p>public function __construct(Context $context, \\Magento\\Framework\\View\\Result\\PageFactory $resultPageFactory)<br \/>\n{<br \/>\n$this-&gt;_resultPageFactory = $resultPageFactory;<br \/>\nparent::__construct($context);<br \/>\n}<\/p>\n<p>public function execute()<br \/>\n{<br \/>\n$resultPage = $this-&gt;_resultPageFactory-&gt;create();<br \/>\nreturn $resultPage;<br \/>\n}<br \/>\n}<\/p><\/blockquote>\n<p>In Magento 2 every action has its own class which implements the execute() method.<\/p>\n<p><strong>Creating a block<\/strong><\/p>\n<p>We will create a simple block class with the getHelloWorldTxt() method which returns the &#8220;Hello world&#8221; string.<\/p>\n<p>1. Create a Helloworld.php file in the app\/code\/Weblizar\/Helloworld\/Block folder with the following code:<\/p>\n<blockquote><p>&lt;?php<br \/>\nnamespace Weblizar\\Helloworld\\Block;<\/p>\n<p>class Helloworld extends \\Magento\\Framework\\View\\Element\\Template<br \/>\n{<br \/>\npublic function getHelloWorldTxt()<br \/>\n{<br \/>\nreturn &#8216;Hello Weblizar, This is your first hello world magento Extension!&#8217;;<br \/>\n}<br \/>\n}<\/p><\/blockquote>\n<p><strong>Creating a layout and template files<\/strong><\/p>\n<p>In Magento 2, layout files and templates are placed in the view folder inside your extension. Inside the view folder, we can have three subfolders: adminhtml, base, and frontend.<br \/>\nThe adminhtml folder is used for admin, the frontend folder is used for frontend and the base folder is used for both, admin and frontend files.<\/p>\n<p>1. First, we will create a helloworld_index_index.XML file in the app\/code\/Weblizar\/Helloworld\/view\/frontend\/layout folder with the following code:<\/p>\n<blockquote><p>&lt;page xmlns:xsi=&#8221;http:\/\/www.w3.org\/2001\/XMLSchema-instance&#8221; xsi:noNamespaceSchemaLocation=&#8221;..\/..\/..\/..\/..\/..\/..\/lib\/internal\/Magento\/Framework\/<br \/>\nView\/Layout\/etc\/page_configuration.xsd&#8221; layout=&#8221;1column&#8221;&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;referenceContainer name=&#8221;content&#8221;&gt;<br \/>\n&lt;block class=&#8221;Weblizar\\Helloworld\\Block\\Helloworld&#8221; name=&#8221;helloworld&#8221; template=&#8221;helloworld.phtml&#8221; \/&gt;<br \/>\n&lt;\/referenceContainer&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/page&gt;<\/p><\/blockquote>\n<p>Every page has a layout hand and for our controller action, the layout handle is helloworld_index_index. You can create a layout configuration file for every layout handle.<\/p>\n<p>In our layout file, we have added a block to the content container and set the template of our block to helloworld.phtml, which we will create in the next step.<\/p>\n<p>2. Create a helloworld.phtml file in the app\/code\/Weblizar\/Helloworld\/view\/frontend\/templates folder with the following code:<\/p>\n<blockquote><p>&lt;h1&gt;&lt;?php echo $this-&gt;getHelloWorldTxt(); ?&gt;&lt;\/h1&gt;<\/p><\/blockquote>\n<p>$this variable is referencing our block class and we are calling the method getHelloWorldTxt() which is returning the string &#8216;Hello world!&#8217;.<\/p>\n<p><strong>And that&#8217;s it. Open the \/helloworld\/index\/index URL in your browser and you should get something like this and <\/strong><\/p>\n<p><a href=\"https:\/\/weblizar.com\/blog\/wp-content\/uploads\/2017\/04\/Magento-helloworld-output-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-5413 size-full\" src=\"https:\/\/weblizar.com\/blog\/wp-content\/uploads\/2017\/04\/Magento-helloworld-output-1.jpg\" alt=\"create an extension in magento output\" width=\"1910\" height=\"1007\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We are going to build a very simple extension in Magento 2. When finished, the extension &#8216;s output will say \u00a0&#8221; Hello Weblizar, This is your first hello world Magento Extension! &#8221; \u00a0in the block content on a custom frontend route.\u00a0Let us learn how to create an extension in magento 2 with hello world as<\/p>\n","protected":false},"author":1,"featured_media":5406,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[61,174,80,6],"tags":[546,547,548,549,550,551,552,553,554,555],"class_list":["post-5379","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-docs","category-ecommerce","category-magento-2","category-plugins","tag-how-to-build-magento-extension","tag-learn-how-to-make-magento-extension","tag-magento","tag-magento-2","tag-magento-2-development","tag-magento-2-extension","tag-magento-extension","tag-magento-extension-tutorial","tag-magento-extensions","tag-simple-magento-extension"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How To Create An Extension In Magento 2 With Hello World Output<\/title>\n<meta name=\"description\" content=\"Magento has always been the first choice for e-commerce business owners and bloggers. In this blog post, we&#039;ll learn how to create an extension in Magento.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/weblizar.com\/blog\/create-an-extension-in-magento-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How To Create An Extension In Magento 2 With Hello World Output\" \/>\n<meta property=\"og:description\" content=\"Magento has always been the first choice for e-commerce business owners and bloggers. In this blog post, we&#039;ll learn how to create an extension in Magento.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/weblizar.com\/blog\/create-an-extension-in-magento-2\/\" \/>\n<meta property=\"og:site_name\" content=\"Weblizar Blog\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/www.facebook.com\/weblizarwp\" \/>\n<meta property=\"article:published_time\" content=\"2017-04-25T10:15:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-03-26T10:54:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/i0.wp.com\/weblizar.com\/blog\/wp-content\/uploads\/2017\/04\/megento-hello-world.jpg?fit=900%2C563&ssl=1\" \/>\n\t<meta property=\"og:image:width\" content=\"900\" \/>\n\t<meta property=\"og:image:height\" content=\"563\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"weblizar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@weblizar\" \/>\n<meta name=\"twitter:site\" content=\"@weblizar\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"weblizar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How To Create An Extension In Magento 2 With Hello World Output","description":"Magento has always been the first choice for e-commerce business owners and bloggers. In this blog post, we'll learn how to create an extension in Magento.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/weblizar.com\/blog\/create-an-extension-in-magento-2\/","og_locale":"en_US","og_type":"article","og_title":"How To Create An Extension In Magento 2 With Hello World Output","og_description":"Magento has always been the first choice for e-commerce business owners and bloggers. In this blog post, we'll learn how to create an extension in Magento.","og_url":"https:\/\/weblizar.com\/blog\/create-an-extension-in-magento-2\/","og_site_name":"Weblizar Blog","article_publisher":"http:\/\/www.facebook.com\/weblizarwp","article_published_time":"2017-04-25T10:15:34+00:00","article_modified_time":"2025-03-26T10:54:43+00:00","og_image":[{"width":900,"height":563,"url":"https:\/\/i0.wp.com\/weblizar.com\/blog\/wp-content\/uploads\/2017\/04\/megento-hello-world.jpg?fit=900%2C563&ssl=1","type":"image\/jpeg"}],"author":"weblizar","twitter_card":"summary_large_image","twitter_creator":"@weblizar","twitter_site":"@weblizar","twitter_misc":{"Written by":"weblizar","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/weblizar.com\/blog\/create-an-extension-in-magento-2\/#article","isPartOf":{"@id":"https:\/\/weblizar.com\/blog\/create-an-extension-in-magento-2\/"},"author":{"name":"weblizar","@id":"https:\/\/weblizar.com\/blog\/#\/schema\/person\/8bd0de51e6e797ea0402f3ffa858a413"},"headline":"How To Create A Hello World Extension In Magento 2","datePublished":"2017-04-25T10:15:34+00:00","dateModified":"2025-03-26T10:54:43+00:00","mainEntityOfPage":{"@id":"https:\/\/weblizar.com\/blog\/create-an-extension-in-magento-2\/"},"wordCount":1028,"commentCount":0,"image":{"@id":"https:\/\/weblizar.com\/blog\/create-an-extension-in-magento-2\/#primaryimage"},"thumbnailUrl":"https:\/\/weblizar.com\/blog\/wp-content\/uploads\/2017\/04\/megento-hello-world.jpg","keywords":["how to build magento extension","learn how to make magento extension","magento","magento 2","magento 2 development","magento 2 extension","magento extension","magento extension tutorial","magento extensions","simple magento extension"],"articleSection":["Docs","eCommerce","Magento 2","Plugins"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/weblizar.com\/blog\/create-an-extension-in-magento-2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/weblizar.com\/blog\/create-an-extension-in-magento-2\/","url":"https:\/\/weblizar.com\/blog\/create-an-extension-in-magento-2\/","name":"How To Create An Extension In Magento 2 With Hello World Output","isPartOf":{"@id":"https:\/\/weblizar.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/weblizar.com\/blog\/create-an-extension-in-magento-2\/#primaryimage"},"image":{"@id":"https:\/\/weblizar.com\/blog\/create-an-extension-in-magento-2\/#primaryimage"},"thumbnailUrl":"https:\/\/weblizar.com\/blog\/wp-content\/uploads\/2017\/04\/megento-hello-world.jpg","datePublished":"2017-04-25T10:15:34+00:00","dateModified":"2025-03-26T10:54:43+00:00","author":{"@id":"https:\/\/weblizar.com\/blog\/#\/schema\/person\/8bd0de51e6e797ea0402f3ffa858a413"},"description":"Magento has always been the first choice for e-commerce business owners and bloggers. In this blog post, we'll learn how to create an extension in Magento.","breadcrumb":{"@id":"https:\/\/weblizar.com\/blog\/create-an-extension-in-magento-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/weblizar.com\/blog\/create-an-extension-in-magento-2\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/weblizar.com\/blog\/create-an-extension-in-magento-2\/#primaryimage","url":"https:\/\/weblizar.com\/blog\/wp-content\/uploads\/2017\/04\/megento-hello-world.jpg","contentUrl":"https:\/\/weblizar.com\/blog\/wp-content\/uploads\/2017\/04\/megento-hello-world.jpg","width":900,"height":563},{"@type":"BreadcrumbList","@id":"https:\/\/weblizar.com\/blog\/create-an-extension-in-magento-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/weblizar.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How To Create A Hello World Extension In Magento 2"}]},{"@type":"WebSite","@id":"https:\/\/weblizar.com\/blog\/#website","url":"https:\/\/weblizar.com\/blog\/","name":"Weblizar Blog","description":"Update yourself with all the latest tech news revolving around wordpress all at one place","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/weblizar.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/weblizar.com\/blog\/#\/schema\/person\/8bd0de51e6e797ea0402f3ffa858a413","name":"weblizar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/83d3ba133e1fd5422595fc5deff6340a4c654d5372f5805eca8c25450ecdef1b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/83d3ba133e1fd5422595fc5deff6340a4c654d5372f5805eca8c25450ecdef1b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/83d3ba133e1fd5422595fc5deff6340a4c654d5372f5805eca8c25450ecdef1b?s=96&d=mm&r=g","caption":"weblizar"}}]}},"_links":{"self":[{"href":"https:\/\/weblizar.com\/blog\/wp-json\/wp\/v2\/posts\/5379","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/weblizar.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/weblizar.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/weblizar.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/weblizar.com\/blog\/wp-json\/wp\/v2\/comments?post=5379"}],"version-history":[{"count":0,"href":"https:\/\/weblizar.com\/blog\/wp-json\/wp\/v2\/posts\/5379\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/weblizar.com\/blog\/wp-json\/wp\/v2\/media\/5406"}],"wp:attachment":[{"href":"https:\/\/weblizar.com\/blog\/wp-json\/wp\/v2\/media?parent=5379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/weblizar.com\/blog\/wp-json\/wp\/v2\/categories?post=5379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/weblizar.com\/blog\/wp-json\/wp\/v2\/tags?post=5379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}