How to create a simple module like HelloWorld in Magento2 without controller
-
were you want to show hello world ? if in admin config you can do this– Waqar AliCommented Feb 21, 2020 at 11:00
-
i'm not sure where, i just wanted to know whether its possible or not to create module without controller. If so can you explain it. Thank in advance.– SathyaCommented Feb 21, 2020 at 11:06
-
yes you can do it.. let me show you in few minutes...– Waqar AliCommented Feb 21, 2020 at 11:08
-
@MohitKumarArora No,They did it with Controller i think so– SathyaCommented Feb 21, 2020 at 11:35
-
yes it's possible to create module without controller.– user83874Commented Feb 21, 2020 at 12:15
2 Answers
I have cerated simple hello world module without controller
Mag/HelloWorld/Block/HomePage.php
<?php
namespace Mag\HelloWorld\Block;
class HomePage extends \Magento\Framework\View\Element\Template
{
public function getMessage()
{
$msg = "Hello World";
return $msg;
}
}
Mag/HelloWorld/etc/module.xml
<?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="Mag_HelloWorld" setup_version="2.0.0"/>
</config>
Mag/HelloWorld/view/frontend/layout/cms_index_index.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Mag\HelloWorld\Block\HomePage" name="mg_helloworld_homepage_block" template="Mag_HelloWorld::homepage.phtml"/>
</referenceContainer>
</body>
</page>
Mag/HelloWorld/view/frontend/templates/homepage.phtml
<h1><?php echo $block->getMessage();?></h1>
<p>Some content here</p>
Mag/HelloWorld/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Mag_HelloWorld',
__DIR__
);
And finally showing on home page check :-
Hope help this code.
Thanks.
here i create a widget module without contoller you can access it to insert widget in admin panel
Vendor\Module\etc\registration.php
registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_Module',
__DIR__
);
Vendor\Module\etc\module.xml
module.xml
<?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="Vendor_Module" setup_version="1.0.0">
</module>
</config>
Vendor\Module\etc\widget.xml
widget.xml
<?xml version="1.0" ?>
<widgets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:widget:Magento_Widget:etc/widget.xsd">
<widget class="Vendor\Module\Block\Widget\Posts" id="widget_posts">
<label>Simple Widget</label>
<description>Posts</description>
<parameters>
<parameter name="posts" sort_order="10" visible="true" xsi:type="text">
<label>Custom Posts Label</label>
</parameter>
</parameters>
</widget>
</widgets>
Vendor\Module\Block\Widget\Posts.php
Posts.php
<?php
namespace Vendor\Module\Block\Widget;
use Magento\Framework\View\Element\Template;
use Magento\Widget\Block\BlockInterface;
class Posts extends Template implements BlockInterface {
protected $_template = "widget/posts.phtml";
}
Vendor\Module\view\frontend\templates\widget\posts.phtml
posts.phtml
<h1>Hello widget </h1>
<?php if($block->getData('posts')): ?>
<h2 class='posts'><?php echo $block->getData('posts'); ?></h2>
<p>This is sample widget. Perform your code here.</p>
<?php endif; ?>
-
yes this works. Thanks– user83874Commented Feb 21, 2020 at 12:31