1

Here is the routes.xml file

<?xml version="1.0"?>
<!--
/**
* Copyright © 2017 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<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>

In Unit3\HelloWorldBlock\Controller\Block there is this Index.php file

/**
 * Class Index
 * @package 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);
}

/**
 * return result
 */
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;
}
}

I have attempted

/unit3helloworldblock/

/unit3helloworldblock/index.php

/unit3helloworldblock/Controller/Block/Index.php

I have checked to make sure the module is enabled, ran upgrade, compiled, flushed cache, and removed generated.

1

Solution

Your url should be formed like this: unit3helloworldblock/block/index (or even unit3helloworldblock/block since your controller name is Index)

Explanation

The url to your controller is formed according to the follow pattern: <front-name>/<controller-name>/<action-name>

front-name

This is the front-name you defined in the routes.xml

Controller-name

This is your folder structure. The router is already going to look inside your Controller folder from your module.

Bear mind if you have multiple levels inside your controller folder. 'Controller/Block/Frontend/Index', your controller name is 'block_frontend;.

Action-name

This is the name of your controller action.

You can find more information here: https://devdocs.magento.com/guides/v2.3/extension-dev-guide/routing.html

1

The url is built according this pattern:

/[frontname]/[controller]/[action]

If the action is called Index that part must not be used. Therefore for your controller called Block and the action Index the url should be:

/unit3helloworldblock/block/

If your controller is called Index (directory) you can use just the frontname for your url:

/unit3helloworldblock/
1

Your URL into this case is,

unit3helloworldblock/block

Or

unit3helloworldblock/block/index

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.