Magento 2 – How to Get the URL of a New Module

base-urllayoutmagento2PHProuting

This is a simple Hello World in a Block, I would like how can I get the URL of this, to check the website this HelloWorld.

I don't know to check my exercises, because I don't know how to get the URL to put in my

I'm starting in the world of magento, and I'm doing several exercises.

\home\cloudpanel\htdocs\magento2.mgt\app\code\Unit3\HelloWorldBlock\Controller\Block\Index.php

<?php
namespace Unit3\HelloWorldBlock\Controller\Block;

class Index extends \Magento\Framework\App\Action\Action
{
    protected $_pageFactory;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $pageFactory
    ) {
        $this->_pageFactory = $pageFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        $layout = $this->_pageFactory->create()->getLayout();
        $block = $layout->createBlock('Unit3\HelloWorldBlock\Block\Test');
        $result = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_RAW);
        $result->setContents($block->toHtml());

        return $result;
    }
}

\home\cloudpanel\htdocs\magento2.mgt\app\code\Unit3\HelloWorldBlock\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="unit3helloworldblock" frontName="unit3helloworldblock">
            <module name="Unit3_HelloWorldBlock" />
        </route>
    </router>
</config>

the result every time is this:
enter image description here

THANKS

Best Answer

Your module URL will load based on the "BaseUrl/frontName/controllerName/actionName"

So as per your module code,

frontName = unit3helloworldblock
controllerName = Block
actionName = Index

So the URL of your module is https://magento2.mgt/unit3helloworldblock/block/index

You can also check on this Magento DevDocs: https://devdocs.magento.com/videos/fundamentals/create-a-new-page/

Hope this will be helpful for you.