Magento – Magento 2 – Correct way of Module Installation

magento2modulePHP

Trying to install sample module in Magento 2 from this. Below is the module structure 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__
);

Executing below command to enable module

php ./bin/magento module:enable Magentostudy_News

It says there are no commands defined in the "module" namespace. Also on frontend it throws below exception. I am using composer but not 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.'

CODE EDIT

Added Setup folder with file InstallSchema.php which includes below code

    <?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. What is the correct way to install Magento 2 modules ?
  2. Any crucial step I missed ?

Reference URL : http://mageinferno.com/blog/setting-up-magento-2-module-right-way-composer-packagist

Best Answer

You can install Magento 2 extension easily from any VCS (github or bitbcuket) if the module is packed with proper composer.json file.

For example, in order to install Easy Template Path Hints 2 extension, you just have to run two commands for installation:

composer config repositories.magesycho-magento2-easy-template-path-hints git git@github.com:MagePsycho/magento2-easy-template-path-hints.git
composer require magepsycho/magento2-easy-template-path-hints:dev-master

Explanation:

1> Registering the module git repository

composer config repositories.magesycho-magento2-easy-template-path-hints git git@github.com:MagePsycho/magento2-easy-template-path-hints.git

Composer config repository command syntax looks like:

composer config repositories.<unique-repo-name> <vcs-type> <vcs-url-https-or-sshl>

Composer will register a new repository to composer.json (under "repositories" node). Updated composer.json looks like:

{  
  "repositories": {
    "magesycho-magento2-easy-template-path-hints": {
      "type": "git",
      "url": "git@github.com:MagePsycho/magento2-easy-template-path-hints.git"
    }
  }
}

2> Registering the module package itself

composer require magepsycho/magento2-easy-template-path-hints:dev-master

Composer require command syntax:

composer require <vendor>/<package>:dev-<branch>

This will add new dependent package under node “require” as:

{
  "name": "magento/magento2ce",
  "description": "Magento 2 (Community Edition)",
  "type": "project",
  "require": {
    "magepsycho/magento2-easy-template-path-hints": "dev-master"
  }
}

and download the module from the repo.

And if you have extension zip file, just extract it and copy it to the app/code/ folder - as that simple.

Once the Module is installed, you can simply enable it using following commands:

php bin/magento module:enable MagePsycho_Easypathhints --clear-static-content
php bin/magento setup:upgrade
Related Topic