Layout 控制器如何在magento 2中加载布局

Layout 控制器如何在magento 2中加载布局,layout,controller,frontend,magento2,Layout,Controller,Frontend,Magento2,我在magento 2中开发了一个示例模块,它只打印hellow世界。只有一个控制器和一个布局文件 <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuratio

我在magento 2中开发了一个示例模块,它只打印hellow世界。只有一个控制器和一个布局文件

    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">        
    <referenceBlock name="content">
        <block 
            template="helloworld.phtml" 
            class="Akhil\Test\Block\Helloworld" 
            name="helloworld_test_helloworld">
        </block>
    </referenceBlock>
</page>  
控制器

  <?php
namespace MageClass\First\Controller\Test;

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

class Helloworld extends \Magento\Framework\App\Action\Action
{

 public function execute()
 {
   $this->_view->loadLayout();
   $this->_view->renderLayout();
 }
}

这里我的疑问是这个布局和控制器是如何链接的。当我通过浏览器默认访问控制器时,这个布局正在加载。如何连接布局和控制器

我想在我的模块中添加另一个布局和控制器。那么,当访问控制器加载所需布局时,如何将它们链接起来

在Magento 2中,控制器和布局文件与其 命名约定

因此,您已经在这里创建了控制器文件:app/code/MageClass/First/controller/Test/Helloworld.php

您的布局文件名为:
app/code/MageClass/First/View/fornted/layout/helloworld\tes‌​t_helloworld.xml

布局文件名始终取决于控制器名称及其操作 名字

布局文件的命名约定为-modulename\u controllername\u actionname.xml

例1:

这里您的模块名称是“Helloworld”控制器名称是“Test”,操作名称是“Helloworld

因此,布局文件名应该是-helloworld\u test\u helloworld.xml(modulename\u controllername\u actionname.xml)

同样的事情现在如果你想创建新的控制器和新的布局,然后你需要再次喜欢使用上述约定

示例:2

在这里,我创建了第二个控制器“Test1”和操作“Helloworld1

因此,您的控制器路径应该是-app/code/MageClass/First/controller/Test1/Helloworld1.php

因此,这里的布局文件名应该是-helloworld\u test1\u helloworld1.xml(modulename\u controllername\u actionname.xml)


更多参考请参考此链接-

此处的布局文件名是什么?控制器文件名和文件夹路径app/code/MageClass/First/controller/Test/Helloworld.phpi我正在了解您的布局文件名,即xml,如hello\u world\u Test.xml。那么你的文件名是什么?哦..我是sry Manthan..控制器文件名和文件夹路径app/code/MageClass/First/controller/Test/Helloworld.php。。。布局文件名和文件夹路径app/code/MageClass/First/View/forntend/Layout/helloworld\u test\u helloworld.xml更正:文件名应该是
routeFrontName\u controllerName\u actionName.xml
而不是
moduleName\u controllerName\u actionName.xml