1

I have doubt as how to create Frontend Module with View template files. I have created Module using this reference Magento Documentation "https://devdocs.magento.com/videos/fundamentals/create-a-new-page/". In this Documentation they have Completed till the Controller section, they haven't explained as how to pass the collected data to view->frontend folder.

Please find the below code for creating frontend module

File path:app/code/VendorName/ModuleName/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="salesperson" frontName="salesperson">
            <module name="VendorName_ModuleName"/>
        </route>
    </router>
</config>

File path:app/code/VendorName/ModuleName/Controller/Login/Index.php

<?php
namespace VendorName\ModuleName\Controller\Login;
class Index extends \Magento\Framework\App\Action\Action
{
    protected $resultPageFactory;
    public function __construct(
       \Magento\Framework\App\Action\Context $context,
       \Magento\Framework\View\Result\PageFactory $resultPageFactory)
{
       $this->resultPageFactory = $resultPageFactory;
       parent::__construct($context);
}

    public function execute()
    {
        return  $resultPage = $this->resultPageFactory->create();
    } 
}

File path:app/code/VendorName/ModuleName/view/frontend/layout/salesperson_login_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">
    <referenceContainer name="content">
        <block class="Magento\Framework\View\Element\Template" name="app_login_page" template="VendorName_ModuleName::salesperson_login_index.phtml" />
    </referenceContainer>
</page>

File path:app/code/VendorName/ModuleName/view/frontend/templates/salesperson_login_index.phtml

<h1>Template file for Frontend Module</h1>

After creating all the above steps, I called the URL as http://domainname.com/salesperson/login/index , It just bring the store default header and footer content without our template code.

Someone help me as to Create View files and pass the data from controller to view files in Magento Custom Frontend Module!!!

Thanks in Advance :)

Regards, Hari

2
  • This will help you. magento.stackexchange.com/questions/297919/… Jan 21, 2020 at 12:22
  • @ChiragParmar , I am asking regarding Frontend View Module. We are not able to get the template page design while loading the frontend URL. The page didn't load the xml file.
    – hari
    Jan 22, 2020 at 15:29

1 Answer 1

0

Name of the module is defined as VendorName_ModuleName

it is necessary to create etc folder and add the module.xml file

app/code/VendorName/ModuleName/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="VendorName_ModuleName" setup_version="1.0.0">
    </module>
</config>

we will add registration.php

app/code/VendorName/ModuleName

registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'VendorName_ModuleName',
    __DIR__
);

we will create a controller to test module but Before create a controller, we will create a route for VendorName_ModuleName module.

To add route, it is necessary to create routes.xml file

app/code/VendorName/ModuleName/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 frontName="salesperson" id="salesperson">
            <module name="VendorName_ModuleName"/>
        </route>
    </router>
</config>

Then, we will continue to create the controller and action

app/code/VendorName/ModuleName/Controller/Login

Index.php

<?php
namespace VendorName\ModuleName\Controller\Login;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\View\Result\PageFactory;

class Index extends Action {

    private $pageFactory;

    public function __construct(
        Context $context,
        PageFactory $pageFactory
    )
    {
        parent::__construct($context);
        $this->pageFactory = $pageFactory;
    }

    public function execute()
    {
        $page = $this->pageFactory->create();
        return $page;
    }
}

now we create a block file

app/code/VendorName/ModuleName/Block

Index.php

<?php

namespace VendorName\ModuleName\Block;

use Magento\Framework\View\Element\Template;

class Index extends Template
{

    public function __construct(
        Template\Context $context,
        array $data = []
    ) {
        parent::__construct($context, $data);
    }
    public function getNameshow()
    {
        $name = "hello i am come from block";
        return $name; 
    }
}

Now we add phtml file

app\code\VendorName\ModuleName\view\frontend\templates

salesperson.phtml

<h1>Template file for Frontend Module</h1>
<h2> <?php echo $block->getNameshow(); ?> </h2>

now last we add Layout file

app\code\VendorName\ModuleName\view\frontend\layout

salesperson_login_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="VendorName\ModuleName\Block\Index" name="show_phtml_data" template="VendorName_ModuleName::salesperson.phtml" />
        </referenceContainer>
    </body>
</page>

run commands

php bin/magento s:up
php bin/magento c:f
php bin/magento c:c 

After creating all the above steps, you called the URL as http://domainname.com/salesperson/login/index

if you have 404 page not found error then you have to enable your module VendorName_ModuleName.

And we will enable this module in this step

php bin/magento module:status

You should see the module is disable now:

List of disabled modules: VendorName_ModuleName

Follow exact guide to enable the module right now, let run the command as:

php bin/magento module:enable VendorName_ModuleName

After this step,

run commands

php bin/magento s:up
php bin/magento s:s:d -f 
php bin/magento c:f
php bin/magento c:c 
6
  • I have also updated the xml file with your content, the only difference is you have added layout='1column' as the page attribute. After updating the XML page content with your code, I have tried the commands you have mentioned also....Its not still working. Can you tell me what else I am missing still....I am getting till the controller, but it is not moving forward to View folder Layout xml file and template phtml file.
    – hari
    Jan 23, 2020 at 11:21
  • @hari I will find problem and Update answer soon
    – Msquare
    Jan 23, 2020 at 14:03
  • Msquare, I want to get this clarified quickly. Since I want to create many frontend view pages using (Controller, Block, Layout and Templates) for my new website.So can you please check and let me know ASAP.
    – hari
    Jan 24, 2020 at 13:29
  • @hari let me know the above solution is working or not for you?
    – Msquare
    Jan 26, 2020 at 4:26
  • Msquare, Thanks the above code works well...Thanks for helping me Msquare... :)
    – hari
    Jan 27, 2020 at 5:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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