Tag: magento2

Creating a Custom Module in Magento 2

In magento2 is fully new structure, truly fast, more structured,use jQuery as a default javascript library. Today I create a new module for magento2.

In Magento 1 we had module declaration files. These lived in


But in magento2 there is no “app/etc/modules/” for module declaration. In magento2 a new module configuration file, located in


In magento2, there is no codepool any more. So you can forgot about codepool concept. lets start to create our first module called “Weblog” and namesapce called “SR”.

Step 1. Create a module configuration file, location is



<pre><?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="SR_Weblog" setup_version="2.0.0">

Step 2. Create a composer.json file inside module in following location

    "name": "sr/module-weblog",
    "description": "N/A",
    "require": {
        "php": "~5.5.0|~5.6.0|~7.0.0",
        "magento/module-store": "100.0.0",
        "magento/module-backend": "100.0.0",
        "magento/module-media-storage": "100.0.0",
        "magento/framework": "100.0.0",
        "lib-libxml": "*"
    "type": "magento2-module",
    "version": "100.0.0",
    "license": [
    "autoload": {
        "files": [ "registration.php" ],
        "psr-4": {
            "SR\\Weblog\\": ""

Step 3. Create registration.php following location SR/Weblog/registration.php


Step 4. Register module. app/etc/config.php can be updated manually by adding the extension to the array of modules with a value of 1 indicating that it is enabled. The following shows adding the new module to the end of the file.

 modules' => array (
  'Magento_Core' => 1,
  'Magento_Wishlist' => 1,
  'SR_Weblog' => 1

If we had pre-installed Magento 2 instance, then we need to run setup update:

php bin/magento setup:upgrade

Clear cache and go to admin->Sores->Configuration->advanced.Here list your new module and look like

Perfect, Our first Weblog module creation is done!

Now I create a controller for displaying something 🙂

Let’s start…

In magento 1, router config is declare inside config.xml file, but in magento2 need to new file which is routes.xml.
So create a new file routes.xml, location is

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="weblog" frontName="weblog">
            <module name="SR_Weblog" />

Configuration is done!

So, now create a new controller file. In magento 1 all controller are locate controllers folder, but magento2 , here no controllers folder. All controller are locate “Controller” folder.

So, Create our index controller inside “Controller” folder, location is


namespace SR\Weblog\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
    public function execute()
        die('Hello World!');

Clear cache, and run, (url should be: http://something/weblog)

That’s it!