自订模块无法在Magento2中运作

cczfrluj  于 2022-11-12  发布在  其他
关注(0)|答案(4)|浏览(136)

我一直在尝试在Magento 2中设置一个基本模块,尽管做了所有理想的更改,它仍然抛出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/magento 2/app/code/Chirag/你好世界/查看/前端/模板/你好世界.phtml

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

任何形式的帮助都将不胜感激。

aiazj4mn

aiazj4mn1#

首先尝试将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.xmlsetup_version声明更改为:

setup_version="2.0.0"

我希望这对你有帮助!

ulydmbyx

ulydmbyx2#

以下更改为我提供了正确答案:
app/code/code/jsp-----------------------------------------------索引控制器-------------------------------------------------------------------------------------------------------------------------------------------

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

/app/code/苹果开发者/苹果开发者/苹果开发者/苹果开发者. 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;
    }
}

布局文件-------------------------------------------------------------------------

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

查看模板-magento 2/app/code/Chirag/你好世界/查看/前端/模板/你好世界.phtml

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

h4cxqtbf3#

您需要按照以下步骤解决问题:
1.您必须将route.xml更改为routes.xml
1.运行安装程序升级命令:
php bin/magento s:up
1.转到your_base_link/helloworld/index/index
1.附上您的结果

8e2ybdfx

8e2ybdfx4#

首先,将route.xml重命名为routes.xml
app/code/Chirag/HelloWorld/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="helloworld" frontName="helloworld">
            <module name="Chirag_HelloWorld" />
        </route>
    </router>
</config>

app/code/functions.php应用程序开发人员-简体中文
你不能在execute方法中使用__construct,这两个方法应该分开。

<?php
/**
 *
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Chirag\HelloWorld\Controller\Index;

use Magento\Framework\App\Action\Action;

class Index extends Action
{
    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $resultPageFactory;

    /**
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     */
    public function __construct(
        \Magento\Framework\App\Action\Context      $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    )
    {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
    }

    /**
     * @return mixed
     */
    public function execute()
    {
        return $this->resultPageFactory->create();
    }
}

当您在layout xml中调用模板时,应该使用下面的格式。
例如:Chirag_HelloWorld::helloworld.phtml
第一个
app/code/Chirag/HelloWorld/Block/HelloWorld.php

<?php
namespace Chirag\HelloWorld\Block;

class HelloWorld extends \Magento\Framework\View\Element\Template
{
    public function getHelloWorldText()
    {
        return __('Hello World');
    }
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

<?= /* @noEscape */ $block->getHelloWorldText() ?>

完成后,运行以下命令以应用更改。

bin/magento setup:upgrade
bin/magento setup:static-content:deploy -f
bin/magento c:f

如果您处于开发人员模式,请使用-f作为参数。
使用bin/magento deploy:mode:show检查模式。
快乐编码!!

相关问题