3

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

2

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
  • Can you please explain a bit more about the role of composer config and require here? How does it work? – amitshree Dec 29 '15 at 14:05
  • 1
    @amitshree updated! – MagePsycho Dec 29 '15 at 14:13
  • @MagePsycho, what if I need to develop a simple hello world module without github or bitbcuket OR a client specific module which will not be on and VCS ? The point here is to have a clear and correct approach to install Magento 2 modules and also to avoid any pitfalls. It would be great if you could shed some light. – Slimshadddyyy Dec 30 '15 at 4:55
  • @Vikram Please check if that sounds helpful. – MagePsycho Dec 30 '15 at 6:29
  • 1
    @MagePsycho so custom code written for almost every custom site-build out there belongs in app/code/ ? Moreover, do all modules regardless of location, should have a composer.json and a registration.php ? – Slimshadddyyy Dec 30 '15 at 7:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.