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

app/etc/modules/*.xml

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

app/code/Packagename/Modulename/etc/module.xml

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

app/code/SR/Weblog/etc/module.xml

 

</pre>
<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">
    </module>
</config>

Step 2. Create a composer.json file inside module in following location
app/code/SR/Weblog/composer.json

{
    "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": [
        "OSL-3.0",
        "AFL-3.0"
    ],
    "autoload": {
        "files": [ "registration.php" ],
        "psr-4": {
            "SR\\Weblog\\": ""
        }
    }
}

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

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'SR_Weblog',
    __DIR__
);

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
SR_Weblog

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

app/code/SR/Weblog/etc/frontend/routes.xml
<?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" />
        </route>
    </router>
</config>

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

app/code/SR/Weblog/Controller/Index/Index.php

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!

7 thoughts on “Creating a Custom Module in Magento 2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s