0

尝试从此在 Magento 2 中安装示例模块。下面是模块结构app/code/NameSpace/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>

作曲家.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\\": ""
        }
    }
    }

注册.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的文件的文件夹InstallSchema.php

    <?php
/**
 * @copyright Copyright (c) 2014 X.commerce, Inc. (http://www.magentocommerce.com)
 */

/* @var $installer \Magento\Setup\Module\SetupModule */

namespace Magentostudy\News\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;

$installer = $this;
$installer->startSetup();

/**
 * Creating table magentostudy_news
 */
$table = $installer->getConnection()->newTable(
    $installer->getTable('magentostudy_news')
)->addColumn(
    'news_id',
    \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
    null,
    ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
    'Entity Id'
)->addColumn(
    'title',
    \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
    255,
    ['nullable' => true],
    'News Title'
)->addColumn(
    'author',
    \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
    255,
    ['nullable' => true,'default' => null],
    'Author'
)->addColumn(
    'content',
    \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
    '2M',
    ['nullable' => true,'default' => null],
    'Content'
)->addColumn(
    'image',
    \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
    null,
    ['nullable' => true,'default' => null],
    'News image media path'
)->addColumn(
    'created_at',
    \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
    null,
    ['nullable' => false],
    'Created At'
)->addColumn(
    'published_at',
    \Magento\Framework\DB\Ddl\Table::TYPE_DATE,
    null,
    ['nullable' => true,'default' => null],
    'World publish date'
)->addIndex(
    $installer->getIdxName(
        'magentostudy_news',
        ['published_at'],
        \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_INDEX
    ),
    ['published_at'],
    ['type' => \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_INDEX]
)->setComment(
    'News item'
);
$installer->getConnection()->createTable($table);
$installer->endSetup();
  1. 安装Magento 2模块的正确方法是什么?
  2. 我错过了任何关键步骤吗?

参考网址: http: //mageinferno.com/blog/setting-up-magento-2-module-right-way-composer-packagist

4

1 回答 1

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:

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

调用Setup而不是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>
于 2015-12-29T10:58:54.550 回答