自定义模块在Magento2中不起作用

时间:2015-08-05 13:19:23

标签: magento magento2

我一直在尝试在Magento2中设置一个基本模块,尽管做了所有理想的更改,它仍然会抛出404。以下是与模块相关的代码。我的供应商名称是 Chirag ,模块名称是 HelloWorld

/var/www/html/magento2/app/code/Chirag/HelloWorld/etc/module.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Chirag_HelloWorld" schema_version="0.0.1" setup_version="0.0.1">
    </module>
</config>

/var/www/html/magento2/app/code/Chirag/HelloWorld/etc/frontend/route.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
    <router id="standard">
        <route id="helloworld" frontName="helloworld">
            <module name="Chirag_HelloWorld" />
        </route>
    </router>
</config>

/var/www/html/magento2/app/code/Chirag/HelloWorld/Controller/Index/Index.php

<?php
/**
 *
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Chirag\HelloWorld\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    /**
     * 
     *
     * @return void
     */
    public function execute()
    {
        protected $resultPageFactory;
        public function __construct(
            \Magento\Framework\App\Action\Context $context,
            \Magento\Framework\View\Result\PageFactory $resultPageFactory
        )
        {
            parent::__construct($context);
            $this->resultPageFactory = $resultPageFactory;
        }

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

/var/www/html/magento2/app/code/Chirag/HelloWorld/Block/HelloWorld.php

<?php
namespace Chirag\HelloWorld\Block;

class HelloWorld extends \Magento\Framework\View\Element\Template
{

}

/var/www/html/magento2/app/code/Chirag/HelloWorld/view/frontend/layout/helloworld_index_index.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Chirag\HelloWorld\Block\HelloWorld" name="helloworld" template="helloworld.phtml" />
        </referenceContainer>
    </body>
</page>

/var/www/html/magento2/app/code/Chirag/HelloWorld/view/frontend/templates/helloworld.phtml

<h1> test hello to Magento 2 !! </h1>

任何形式的帮助都会非常感激。

3 个答案:

答案 0 :(得分:4)

首先尝试将route.xml重命名为routes.xml,看看是否能解决问题。

接下来尝试更改控制器中的代码,尝试更改公司/模块名称:

<?php

namespace YourCompany\ModuleName\Controller\Index;

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
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
    }

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

同样在您的helloworld_index_index.xml中,您可以尝试将模板修饰更改为:

template="YourCompany_ModuleName::templatename.phtml"

最后,您可以尝试将module.xml setup_version声明更改为:

setup_version="2.0.0"

我希望这有帮助!

答案 1 :(得分:1)

以下更改为我提供了正确答案:

索引控制器 - magento2 / app / code / Chirag / HelloWorld / Controller / Index / Index.php

<?php
/**
 *
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Chirag\HelloWorld\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    /**
     * Show Contact Us page
     *
     * @return void
     */
    public function execute()
    {
        $this->_view->loadLayout();
        $this->_view->getLayout()->getBlock('helloworld');
        $this->_view->renderLayout();
    }
}

阻止 - magento2 / app / code / Chirag / HelloWorld / Block / Question.php

<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Chirag\HelloWorld\Block;

use Magento\Framework\View\Element\Template;

/**
 * Main contact form block
 */
class Question extends Template
{
    /**
     * @param Template\Context $context
     * @param array $data
     */
    public function __construct(Template\Context $context, array $data = [])
    {
        parent::__construct($context, $data);
        $this->_isScopePrivate = true;
    }
}

布局文件 - magento2 / app / code / Chirag / HelloWorld / view / frontend / layout / helloworld_index_index.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Chirag\HelloWorld\Block\Question" name="helloworld" template="Chirag_HelloWorld::helloworld.phtml">
            </block>
        </referenceContainer>
    </body>
</page>

查看模板 - magento2 / app / code / Chirag / HelloWorld / view / frontend / templates / helloworld.phtml

<?php echo 'helloworld view'; ?>
<h1> Hello World </h1>

答案 2 :(得分:0)

  1. 您必须将route.xml更改为route.xml

  2. 运行安装程序升级命令:

    php bin / magento s:up

  3. 转到your_base_link / helloworld / index / index

  4. 添加结果