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

How controller loads the layout in magento 2

我在 magento 2 中开发了一个示例模块,它只打印 hellow world.there is only one controller and one layout file

控制器

  <?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();
 }
}

布局文件

    <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>  

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

我想在我的 module.so 中添加另一个布局和控制器 如何 link 当访问控制器时加载所需的布局

In Magento 2 Controller and layout file linked together with their naming conventions.

所以你在这里创建了你的控制器文件,它是:app/code/MageClass/First/Controller/Test/Helloworld.php

您的布局文件名为:
app/code/MageClass/First/View/forntend/layout/helloworld_tes‌ t_helloworld.xml

Layout file name always depends on the controller name and its action name.

布局文件的命名规则是 - modulename_controllername_actionname.xml

Example 1:

这里你的模块名称是“Helloworld”控制器名称是“Test”你的动作名称是“ Helloworld".

所以你的布局文件名应该是 - helloworld_test_helloworld.xml(modulename_controllername_actionname.xml)

同样的事情,如果你想创建新的控制器和新的布局,那么你需要再次喜欢使用上面的约定

Example :2

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

所以你的控制器路径应该是 - app/code/MageClass/First/Controller/Test1/Helloworld1.php

所以这里你的布局文件名应该是 - helloworld_test1_helloworld1.xml(modulename_controllername_actionname.xml)

更多参考请参阅此 link - http://devdocs.magento.com/guides/v2.0/frontend-dev-guide/layouts/layout-types.html