How to create a simple module in Magento 2.0

Preview:

Citation preview

How to create a module in Magento 2.0 (Updated in August 2015)

2

1 THE STRUCTURE OF A SIMPLE MODULEMagento 1 vs Magento 2Some new folders in Magento 2

2CREATE A SIMPLE MODULE IN MAGENTO 2Step 1: Create configuration filesStep 2: Create controller, layout and template filesStep 3: Activate the module in the configuration file

OUTLINE

MAGENTO 2

Folders and files of a module are put in only a package and located in app/code folder

1. THE STRUCTURE OF A SIMPLE MODULE

MAGENTO 1Folders and files of a module are located in different folders: app/code/local, app/code/community, app/design, app/etc, app/locale, js, skin,…

Some new folders in Magento 2

i18n: store the .csv files for module translator. Eg: en_US.csv, de_DE.csv

Setup: store the files which are used to create tables or insert data to the database.

View: This folder stores the layout, template, image, CSS and Javascript files which are used for your module.

Let’s create folders with the structure as following:

Namespace: TutorialModule name: ExampleExample link on Local host: http://localhost/magento2/example/index/index/

2. CREATE A SIMPLE MODULE IN MAGENTO 2

Step 1: Create configuration files

1.1. Create file: app/code/Tutorial/Example/etc/module.xmlPurpose: Declare your moduleSource Code:

(Go to our blog tutorial for full source code)

Step 1: Create configuration files

1.2 Create file: app/code/Tutorial/Example/etc/frontend/routes.xml Purpose: Declare the router of module for frontend

(Go to our blog tutorial for full source code)

Step 2: Create controller, layout and template files

2.1 Create the controller file: app/code/Tutorial/Example/Controller/Index/Index.php

=> The souce code is found in our blog tutorial

Step 2: Create controller, layout and template files

2.2 Create the layout file: app/code/Tutorial/Example/view/frontend/layout/

example_index_index.xml

(Go to our blog tutorial for full source code)

Step 2: Create controller, layout and template files

2.3 Create the template file: app/code/Tutorial/Example/view/frontend/templates/index.phtml

(Go to our blog tutorial for full source code)

Step 3: Activate the module in the configuration file

3.1 Activate module by opening app/etc/config.php file then add this line “'Tutorial_Example' => 1” into it:

(Go to our blog tutorial for full source code)

Step 3: Activate the module in the configuration file

3.2 Open the Command in Windows (or the Terminal in Linux and MAC OS). Go to Magento root folder and run this command line to install module:

bin\magento setup:upgrade

(Go to our blog tutorial for full source code)

Step 3: Activate the module in the configuration file

3.3 Clear the Magento cache then access with url http://localhost/magento2/example/index/index/

2

Following MageWorld Magento 2 Tutorial Series…

Recommended