Pages

Thursday, September 17, 2015

Magento 2 : How to add custom top links in header



  • Steps to add custom link for CMS page in magento 2 header are as under
  • First create a CMS page for example we will create an about us page with identifier as “about-us”.
  • Now usually, to access the cms page in frontend, we use www.domain.com/{identifier}
  • So, the top link that we will add for the about us page should redirect to www.domain.com/about-us
  • To add the top link, we will add following code in default.xml of our theme



Path to the file: app/design/frontend/Magento/themename\Magento_Theme\layout\default.xml


<referenceBlock name="top.links">
    <block class="Magento\Framework\View\Element\Html\Link\Current" name="about-us">
       <arguments>
           <argument name="label" xsi:type="string">About Us</argument>
           <argument name="path" xsi:type="string">about-us</argument>
       </arguments>
   </block>
</referenceBlock>

Magento 2: Create Simple Hello World Module

Magento2 beta version has been released by magento for developer understanding. We can get the setup from Git Gub https://github.com/magento/magento2

You can get setup guide from http://www.ubertheme.com/magento-news/magento-2-0-installation-guide/

In Magento2 there is a drastic change in the structure of the code. All the code pools are removed and also the Skin directory. There are many changes related to the theme folder as well. To understand how the structure of Magento2 works, let us check it by creating a simple helloworld module.

Before starting the code section, let us create the directory structure that we will need.

app/code/stepbystepprograming/Helloworld
app/code/stepbystepprograming/Helloworld/etc
app/code/stepbystepprograming/Helloworld/etc/frontend
app/code/stepbystepprograming/Helloworld/Controllers
app/code/stepbystepprograming/Helloworld/view/frontend/layout
app/code/stepbystepprograming/Helloworld/view/frontend/templates


You can skip the Block directory if required. Will explain it in the end of the process. For now we will also create Block directory as under:

app/code/stepbystepprograming/Helloworld/Block

Now, as we have the directory structure ready, we will now create file as per module requirement in given sequence:

1.First, we have to create the module configuration file named module.xml in app/code/stepbystepprograming/Helloworld/etc

The content for the file will be as under, if it does not depend on any other module:


<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="stepbystepprograming_Helloworld" setup_version="2.0.0" />
</config>
But, if our module depends on any other module then we will use a tag as under


<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="stepbystepprograming_Helloworld" setup_version="2.0.0" >
<sequence>

        <module name="Magento_Eav"/>

        <module name="Magento_Directory"/>

    </sequence>

</module>
</config>

2. Now, we will create a route configuration file named routes.xml in app/code/stepbystepprograming/Helloworld/etc/frontend

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
    <router id="standard">
        <route id="helloworld" frontName="helloworld">
            <module name="stepbystepprograming_Helloworld" />
        </route>
    </router>
</config>

3. As we have defined the route now, we will create the controller file. In Magento2, we need to create separate file for each action under the controller folder.


In our case we will create Index.php file in app/code/stepbystepprograming/Helloworld/Controller/Index

In every action file there will be a method name excute() that will be invoked when the action is called.


<?php

namespace stepbystepprograming\Helloworld\Controller\Index;


class Index extends \Magento\Framework\App\Action\Action
{
    /**
     * Index action
     *
     * @return $this
     */
    public function execute()
    {
$this->_view->loadLayout();
       $this->_view->renderLayout();
    }
}
4. Further, we will create a Block file named Helloworld.php in app/code/stepbystepprograming/Helloworld/Block

<?php

namespace stepbystepprograming\Helloworld\Block;


class Helloworld extends \Magento\Framework\View\Element\Template
{

}

5. Now, as we have our Controller and Block ready, we will create layout file named helloworld_index_index.xml in app/code/stepbystepprograming/Helloworld/view/frontend/layout


Here we will create separate file for each action in layout.


<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="stepbystepprograming\Helloworld\Block\HelloWorld" name="helloworld" template="helloworld.phtml" />
        </referenceContainer>
    </body>
</page>


6. Further, we will create our phtml file that will be called named helloworld.phtml in app/code/stepbystepprograming/Helloworld/view/frontend/templates


<h1>Hello World!</h1>
<div class="helloworld-cms">
    <?php echo __('You are visiting the page of hello world'); ?>
</div>



7. Finally, as all our module files are created we will activate our module by appending following code in the module array in app/etc/config.php



 'Magento_Wishlist' => 1,
 'stepbystepprograming_Helloworld' => 1,



8. Clear chache and be sure that you also delete the view_preprocessed directory that is created in var/

We can check the out put using www.domain.com/helloworld

As it is stated above that we can skip the block directory, we have to replace the layout xml file content with following this works similar to core/template that was in Magento 1.x


<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Magento\Framework\View\Element\Template" name="helloworld" template="stepbystepprograming_Helloworld::helloworld.phtml" />
        </referenceContainer>
    </body>
</page>


We have developed the Helloworld simple module successfully.

Cheers and Good Luck.