We are going to build a very simple extension in Magento 2. When finished, the extension ‘s output will say ” Hello Weblizar, This is your first hello world Magento Extension! ” in the block content on a custom frontend route. Let us learn how to create an extension in magento 2 with hello world as our output.
At list, you have latest Magento 2 version which is currently 2.1. Is installed on your local system.
Before we start a Magento 2 extension development, there are two things people often forget and we recommend you to do:
1. Disable Magento cache
Disabling Magento cache during development will save you some time because you won’t need to manually flush the cache every time you make changes to your code.
The easiest way to disable cache is to go to Admin => System => Cache Management => select all cache types and disable them.
2. Put Magento into a developer mode
You should put Magento into a developer mode to ensure that you see all the errors Magento is throwing at you.
In order to do this, open your terminal and go to the Magento 2 root. From there you should run the following command:
php bin/magento deploy:mode:set developer
Creating the extension files and folders:
The first step is to create the extension folder and necessary files required to register a Magento extension.
1. Create the following folders:
The Weblizar folder is the extension namespace, and Helloworld is the extension name.
Note: If you don’t have the code folder in your app directory, create it manually.
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:
<config xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=”urn:magento:framework:Module/etc/module.xsd”>
<module name=”Weblizar_Helloworld” setup_version=”1.0.0″></module>
3. To register the module, create a registration.php file in the app/code/Weblizar/Helloworld folder with the following code:
4. Open your terminal and go to the Magento 2 root. Run from there the following command:
php bin/magento setup:upgrade
If you want to make sure that the extension is installed, you can go to Admin => Stores => Configuration => Advanced => Advanced and check that the extension is present in the list or you can open app/etc/config.php and check the array for the ‘Weblizar_Helloworld’ key, whose value should be set to 1.
Creating a controller
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:
<config xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=”urn:magento:framework:App/etc/routes.xsd”>
<route id=”helloworld” frontName=”helloworld”>
<module name=”Weblizar_Helloworld” />
Here we are defining our frontend router and route with an id ” helloworld “.
The frontName attribute is going to be the first part of our URL.
In Magento 2 URL’s are constructed this way:
So in our example, the final URL will look like this:
2. Now we create the Index.php controller file in the app/code/Weblizar/Helloworld/Controller/Index folder with the following code:
class Index extends \Magento\Framework\App\Action\Action
public function __construct(Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory)
$this->_resultPageFactory = $resultPageFactory;
public function execute()
$resultPage = $this->_resultPageFactory->create();
In Magento 2 every action has its own class which implements the execute() method.
Creating a block
We will create a simple block class with the getHelloWorldTxt() method which returns the “Hello world” string.
1. Create a Helloworld.php file in the app/code/Weblizar/Helloworld/Block folder with the following code:
class Helloworld extends \Magento\Framework\View\Element\Template
public function getHelloWorldTxt()
return ‘Hello Weblizar, This is your first hello world magento Extension!’;
Creating a layout and template files
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.
The 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.
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:
<page xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=”../../../../../../../lib/internal/Magento/Framework/
<block class=”Weblizar\Helloworld\Block\Helloworld” name=”helloworld” template=”helloworld.phtml” />
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.
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.
2. Create a helloworld.phtml file in the app/code/Weblizar/Helloworld/view/frontend/templates folder with the following code:
<h1><?php echo $this->getHelloWorldTxt(); ?></h1>
$this variable is referencing our block class and we are calling the method getHelloWorldTxt() which is returning the string ‘Hello world!’.
And that’s it. Open the /helloworld/index/index URL in your browser and you should get something like this and
We wish you Happy Independence Day 2016 to all our readers.India is celebrating 70th independence day on August 15th.
The day is observed to celebrate the nation’s freedom from the British rule in 1947, a movement led by personalities like Mahatma Gandhi, Jawaharlal Nehru, Bhagat Singh, Sardar Vallabhai Patel, Lala Lajpat Rai, Chandra Shekhar Azad, Gopal Krishna Gokhale, Sarojini Naidu, Lokmanya Tilak, among many others.
Freedom in the mind,
Faith in the words..
Pride in our souls..
Let’s salute the nation on 69th Independence Day!
Get the DISCOUNT of “10%” on your next premium purchase. Coupon Code : IDSPL10%OFF
Today Weblizar released, new plugin Responsive Coming Soon Page .
“Responsive Coming Soon Page” will useful for your new and existing WordPress site. This plugin have coming soon landing page for WordPress site. We provide the number of features in free plugin.
Features Of Plugin
Responsive Coming Soon Page Plugin For WordPress
Merry Christmas and A very Happy & Prosperous New Year …
We are Offering a BIG 10% Discount on our All WordPress Premium Products.
Coupon Code : MerryChristmas
Weblizar wish A Very Happy Diwal to all our valuable customers.
Celebrate this Diwail with Weblizar and get amazing discount on 10th – 15th November 2015.
We are offering a small gift to all our new & existing customers on this prosperous occasion.
coupon code: HappyDiwali
Use this coupon code to get 10% OFF on all Weblizar Premium products.+
What is Twitter widget id?
Twitter widget id is associated with your twitter account. Widget id is used for fetch your twitter tweets from your account. Various plugins, extension, scripts use twitter widget is to get the information from your account. The information fetched using widget id only the public visible, it’s not your private or confidential information. And this is a very secure and authentic way to access information because program usage your own id so information is fetched only from your account only.
Follow the step by step instruction to get your own account Widget ID
Step 1) The first step is log in to your Twitter account. Click on LINK to go on the login screen of twitter. And enter your twitter email or password in the first field and password in the second field.
Step 2) Copy username as shown below –
Step 3) Congrats! Finally, you are done. Start using it into your twitter plugin, extension or code.
Thanks for reading our blog about Getting Twitter Widget ID.
Create a gallery with grid view using our Gallery Pro plugin.
With Gallery Pro, you can Add unlimited images on your blog site. Design various types of image, video, link gallery using this plugin. Gallery Pro provide you light box image preview and two gallery layout with various fonts.
Using [WG id=xx] shortcode, you can publish image or photo gallery into any Page or Post in your WordPress sites. You can use multiple gallery shortcodes on multiple pages and posts to display various images galleries.
Multiple Gallery Option (new)
Multiple Gallery Shortcode (new)
Simple & Very Easy Admin Gallery Dashboard
Grid Gallery Effect
Two Design Layout
Three Types of Color Scheme
Light Box Integrated
Add Unlimited Images into Gallery
Multilingual Translation Ready
Add multiple youtube videos
Add multiple Vimeo videos
Add multiple External links
Check Out Free Plugin Demo: Gallery Pro