Magento 2 - 正确的模块安装方式

时间:2015-12-29 10:34:23

标签: php magento magento2 magento-2.0

尝试从this在Magento 2中安装样本模块。以下是模块结构app/code/NameSpace/Module/

module image

module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Magentostudy_News" setup_version="0.0.1" schema_version="0.0.1"/>
</config>

composer.json

{
    "name": "magentostudy/module-news",
    "description": "N/A",
    "require": {
        "php": "~5.5.0|~5.6.0|~7.0.0",
        "magento/module-store": "100.0.0",
        "magento/module-email": "100.0.0",
        "magento/module-ui": "100.0.0",
        "magento/framework": "100.0.0"
    },
    "type": "magento2-module",
    "version": "0.0.1",
    "license": [
        "OSL-3.0",
        "AFL-3.0"
    ],
    "autoload": {
        "files": [ "registration.php" ],
        "psr-4": {
            "Magentostudy\\News\\": ""
        }
    }
    }

为registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Magentostudy_News',
    __DIR__
);

执行以下命令启用模块

php ./bin/magento module:enable Magentostudy_News

它说there are no commands defined in the "module" namespace.同样在前端它会抛出异常。我正在使用作曲家但不是github。

Fatal error: Uncaught exception 'Magento\Framework\Exception\LocalizedException' with message 'Source class "\Magento\Framework\Module\Updater\Setup" for "Magento\Framework\Module\Updater\SetupFactory" generation does not exist.'

代码编辑

添加了文件Setup的{​​{1}}文件夹,其中包含以下代码

InstallSchema.php
  1. 安装 Magento 2 模块的正确方法是什么?
  2. 我错过了任何关键步骤?
  3. 参考网址:http://mageinferno.com/blog/setting-up-magento-2-module-right-way-composer-packagist

1 个答案:

答案 0 :(得分:0)

你好这里有两个正确的你需要尝试的模块

1:更正 composer.json

这是基本的例子:

{
"name": "magentostudy/module-news",
"description": "N/A",
"require": {
    "php": "~5.5.0|~5.6.0|~7.0.0",
    "magento/module-store": "100.0.0",
    "magento/module-email": "100.0.0",
    "magento/module-ui": "100.0.0",
    "magento/framework": "100.0.0"
},
"type": "magento2-module",
"version": "0.0.1",
"license": [
    "OSL-3.0",
    "AFL-3.0"
],
"autoload": {
    "files": [ "registration.php" ],
    "psr-4": {
        "Magentostudy\\News\\": ""
    }
}

}

2:

在您的目录中,有一个文件夹缺失

名为设置,而不是 sql

了解更多详情阅读这些模块:

https://github.com/magento/magento2-samples

输出安装文件名应为:InstallSchema.php

代码如下:

namespace Magentostudy\News\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

class InstallSchema implements InstallSchemaInterface
{
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;

        $installer->startSetup();
        $table  = $installer->getConnection()
            ->newTable($installer->getTable('test_helloworld'))
            ->addColumn(
                'id',
                \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                null,
                ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
                'Id'
            )
            ->addColumn(
                'label',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                null,
                ['default' => null, 'nullable' => false],
                'Name'
            )
            ->addColumn(
                'value',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                null,
                ['default' => null, 'nullable' => false],
                'Stores'
            );
        $installer->getConnection()->createTable($table);
        $installer->endSetup();
    }
}

尝试像这样编辑modules.xml文件

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Magentostudy_News" setup_version="0.0.1">

    </module>
</config>