2017 © Pedro Peláez
 

magento2-module module-m2-extension

Bare-bones demo extension

image

m2demo/module-m2-extension

Bare-bones demo extension

  • Friday, January 16, 2015
  • by coldgreentea
  • Repository
  • 10 Watchers
  • 40 Stars
  • 52 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 7 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Reference Module

Thank you for trying the Magento reference module! We created it to get you started with module development as quickly as possible. Right now it simply displays a list of Magento modules but you can use it to:, (*1)

  • See the basic components of a Magento module and where they belong
  • Do a quick installation
  • See it work

Installing and verifying the reference module

You can install the reference module in any of the following ways:, (*2)

  1. By copying the code to your <your Magento install dir>/app/code/<VendorName>/<ModuleName> directory.
    This method requires some manual tasks but it easy.
  2. Using composer update.

Installing the module by copying code

Any Magento module requires a particular directory structure under <your Magento install dir>/app/code. The structure starts with:, (*3)

<VendorName>/<ModuleName>

The reference module requires the following structure:, (*4)

M2demo/M2Extension

To add the module to your Magento installation:, (*5)

  1. Log in to your Magento server as a user with privileges to write to the web server docroot (typically either the root user or the web server user).
  2. Enter the following commands in the order shown:, (*6)

    cd <your Magento install dir>/app/code
    mkdir -p M2demo/M2Extension
  3. Go to the reference module GitHub., (*7)

  4. Click Download Zip.
  5. Copy the .zip file you downloaded to your Magento server's <magento install dir>/app/code/m2demo/module-m2-extension directory.
  6. Enter the following commands in the order shown:, (*8)

    unzip m2extension-master.zip
    mv m2extension-master/* .
    rm -rf m2extension-master
  7. Open <your Magento install dir>/app/etc/config.php in a text editor., (*9)

  8. Add the following anywhere under: 'modules' => array (:, (*10)

    'M2Demo_M2Extension' => 1
  9. Save your changes and exit the text editor., (*11)

Installing the module using Composer

To install any module using composer update, you must modify the Magento 2 composer.json. The changes required for the reference module are simple, and after that it installs automatically., (*12)

To use Composer to install the module:, (*13)

  1. Log in to your Magento server as a user with root privileges., (*14)

    Note: On Ubuntu, you might need to use the sudo su command., (*15)

  2. Change to your Magento installation directory., (*16)

  3. Open composer.json in a text editor.
  4. Add the following line in the "require": section:, (*17)

    "m2demo/module-m2-extension": "*"

    Note: Make sure the preceding line ends with a comma character., (*18)

  5. In the autoload": { "psr-4" section, add the following line:, (*19)

    "M2Demo\\": "app/code/M2Demo/"

    Note: Make sure the preceding line ends with a comma character., (*20)

  6. Save your changes and exit the text editor., (*21)

  7. From your Magento installation directory, enter the following commands in the order shown:, (*22)

    composer dump-autoload
    composer update

Verifying the reference module

  1. Log in to your Magento server as a user with root privileges.
  2. Enter the following commands in the order shown to clear the Magento cache:, (*23)

    cd <your Magento install dir>/var
    rm -rf cache page_cache
  3. If you're currently logged in to the Magento Admin, log out., (*24)

  4. Log in to the Magento Admin as an administrator.
  5. Click Stores > Configuration > ADVANCED > Advanced > Disable Modules Output.

If m2demo_module-m2-extension displays in alphabetical order, you successfully installed the reference module!, (*25)

To see the reference module at work, enter the following URL in your browser's address or location field:, (*26)

    http[s]://<your web server IP or host name>/<your Magento base dir>/demo_extension/index/sayhello

For example:, (*27)

    http://www.example.com/magento/demo_extension/index/sayhello

A list of Magento modules displays. That's it! You're done!, (*28)

Components

composer.json

Root

To autoload classes in the module more quickly, the root composer.json must be updated. Place the following line in the autoload->psr-4 section:, (*29)

"M2Demo\\": "app/code/M2Demo/"

For reference, see Composer's autoloading documentation., (*30)

Module

To integrate the module with composer, a composer.json file must be placed in the root of the extension., (*31)

Routing

A router takes the given request and determines which controller and action should handle it. Routing options are configured in "etc/routes.xml" (relative to extension root)., (*32)

Router

The router element's id attribute determines which router to use for this module., (*33)

Route

The route element's id attribute provides a unique name for the router and module pair. Used for merging config information of multiple routers., (*34)

Frontname

The module element's frontname attribute is what will show up in the url corresponding to the module., (*35)

Area

During runtime, one of several areas is always active, depending on the type of user and code being executed. This module is meant for the frontend area. The directory structure is: Extension/etc/area/routes.xml, (*36)

module.xml

The module.xml file defines the name of the module. If any of the module's dependencies need to be loaded in a particular sequence, this file can also store that sequence., (*37)

Controller

The controller class is in app/code/M2Demo/M2Extension/Controller/Index/SayHello.php., (*38)

URL

The controller is accessed at the URL /demo_extension/index/sayhello * demo_extension is the frontname of the module * index is the namespace of the controller (slightly different from the namespace of the class but related). * sayhello is the action name., (*39)

Code

The controller class extends \Magento\Framework\App\Action\Action. It implements an execute() method, which contains the controller's business logic. In this case, it prints out a short message and a list of active modules. If it renders output, the output must go into the _response object., (*40)

Response

The _response object stores the information that is sent back to the client. It is a protected member of the class defined by a parent., (*41)

Parent

The parent's dispatch() method calls execute(), and then returns the _response back to the system, which delivers it to the client., (*42)

The Versions

16/01 2015

dev-master

9999999-dev

Bare-bones demo extension

  Sources   Download

The Requires

 

18/12 2014

v2.0

2.0.0.0

Bare-bones demo extension

  Sources   Download

The Requires

 

16/12 2014

1.0

1.0.0.0

Bare-bones demo extension

  Sources   Download

The Requires