Tag: magento 2 module creation

How to create admin menu in magento 2

My previous tutorial show you to create module in magento 2, Click here. Today I discuss about magento 2 admin menu.

Suppose module called “SR” and namesapce called “Weblog”.

In Magento 1, menu configuration are locate inside adminhtml.xml but in Magento 2 configuration is locate menu.xml file, location is

app/code/SR/Weblog/etc/adminhtml/menu.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
    <menu>
        <add id="SR_Weblog::blogpost" title="Weblog blogpost" module="SR_Weblog" sortOrder="300" parent="Magento_Backend::content_elements" action="weblog/blog" resource="SR_Weblog::blogpost"/>
    </menu>
</config>

Done!

Clear magento cache, and go to admin panel. You can change the menu, your new menu is appended with(Content -> Weblog blogpost).

Now you create a admin controller, file location is

app/code/SR/Weblog/Controller/Adminhtml/Blog/Index.php
<?php
namespace SR\Weblog\Controller\Adminhtml\Blog;

class Index extends \Magento\Backend\App\Action
{
    /**
     * Index action
     *
     * @return void
     */
    public function execute()
    {
        die('Hello World!');
    }
}

Create Router
app/code/SR/Weblog/etc/adminhtml/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="admin">
        <route id="weblog" frontName="weblog">
            <module name="SR_Weblog" before="Magento_Adminhtml" />
        </route>
    </router>
</config>

Clear magento cache

Now you browse Content ->Weblog blogpost, yes it’s working.

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!