2

I just created my first own module. It is a Block that should be shown on the cms home page. Later on I want to change this into a custom navigation bar that is present on all pages of my shop but for now I just want to see that it is working so far.So I followed this simple tutorial:http://magestore.com/magento-2-tutorial/magento-2-block-create-template-block/ Some things were unclear but I think I figured them out. Everything should work now but I can't see the message "Hello World" that my .php returns. Here is my setup of all files:

In app/code/Test/Navbar my folder structure looks like this:

├── Navbar ├── Block ├── Navbar.php ├── etc ├── module.xml ├── view ├── frontend ├── layout ├── cms_index_index.xml ├── templates ├── default ├── navbar.phtml ├── registration.php

Here are the contents of my files:

registration.php

<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

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

Navbar.php

/**
* Room23 navigation bar
*/
class Navbar extends \Magento\Framework\View\Element\Template 
{
public function getTitle() 
{
return "Hello World";
}
}

module.xml

<?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="Test_Navigation" setup_version="1.0.0"/>
</config>

cms_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="main">
<block class="Test\Navbar\Block\Navbar" template="default/navbar.phtml"/>
</referenceContainer>
</body>
</page>

navbar.phtml

<?php
/**
* Test navbar template
*
* @var $block TestNavbarBlockNavbar
*/
?>
<h1><?php echo $block->getTitle(); ?></h1>

I successfully enabled the module (I am running Magento 2 on a MAMP local server btw) and I don't get any errors. I read not only this but also other tutorials and they all tell me to do the same thing. But I don't see any results on my homepage. Why?

0

let try this,

cms_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Test\Navbar\Block\Navbar" name="custom.block" template="Test_Navigation::default/navbar.phtml"/>
</referenceContainer>
</body>
</page>

module.xml

<?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="Test_Navigation" setup_version="1.0.0">
   <sequence>
    <module name="Magento_Cms"/>
   </sequence>
   </module>
</config>

note: you declared module name Test_Navigation in registration.php but your folder structure is app/code/Test/Navbar but keep it like magento way like app/code/Test/Navigation it avoid some unnecessary confusion. Please take a look of Magnto_Cms & Magento_Catalog_Search module registration.php module name and their folder structure.

  • Thank you so much! That actually fixed it. Now since I got really confused by all the tutorials: Do you know how to display the block at top of every page? I have to use the default.xml layout xml, right? Also how do I implement any html there? I want the navigation bar of my shop look exactly like the one on my normal html website where the shop is linked. – Pixell Sep 15 '16 at 20:21
  • yes, default.xml is loaded on all pages of the Magento site, so you can get the block you are looking to create on all pages that way, just by referencing the container you want it to be in. As for the html, that can be placed in the phtml template that is being loaded. All html, javascript and php code is valid in those files. Just copy that code over and add in your styles to the stylesheets and you should be good to go. – circlesix Sep 16 '16 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.