📜 ⬆️ ⬇️

Module example for Magento 2



This article is out of date. Here is a new one .



In the week I felt an example of creating a module for Magento 2. In the course of probing, I tried to create a working environment by analogy with the environment we had developed at the moment for developing modules for Magento 1.


Under the cat link to the github-project with an example of a module for Magento 2 with (semi) automatic deployment of the environment for its development.

useful links


I do not describe the system requirements for Magento 2, how to install Composer, set up PHP and create a database. Similar general information is here on these links or is searched on Google:
')

Module itself


Link to the project on github: sample_mage2_module

Catalogs



(gray indicates files and directories that are not under version control, they are generated during deployment or manually created)

We place the code for the module itself in the ./src subdirectory.

We deploy the development environment for the development of the module in the ./work/ directory.

The ./work/cfg/ directory contains the source code for the script (s) for deploying the developer version of the Magento application.

The ./work/cfg/templates.json file is one of the two configuration files of our composer_plugin_templates plugin which, according to the composer events (for example, composer install ), copies the source file templates (under version control) to the target directory with the replacement in placeholder templates their real values ​​for this instance of the application. You can do it manually, but we use this plugin. In this example, we need to create from the ./work/cfg/bin/deploy/post_install.sh template just one shell script ( ./work/bin/deploy/post_install.sh ), which will raise the database for the Magento application after working out "composer install" .

The ./work/htdocs/ directory is the root of our web application, Magento2 ( copy strategy) is deployed in it, and our module files are superimposed on this scan ( symlink strategy). Please note that files in ./work/htdocs/ are linked not from the ./src/ directory, but from the ./work/vendor/flancer32/sample_mage2_module/src directory.

The file ./work/.gitignore belongs to the directory ./work/ (working environment for the development of the module).

File ./work/composer.json - instructions for deploying a working environment through Composer.

The ./work/templates.json.init file is a template for the second configuration file used in our composer composer_plugin_templates plugin. In the plugin, we use two configuration files: in one we define the list of templates and addresses where we need to place the finished files after replacing the variables with their values ​​for this instance of the sweep, and in the second - the values ​​of these variables. If the first configuration file does not depend on where we are deploying the application (and is under version control), then the second one depends, and therefore is taken out of control. Instead, control is the preparation for its creation.

The ./.gitignore file refers to the entire project (with the exception of the ./work. Subdirectory ).

The ./composer.json file contains instructions for deploying our Magento2 module (the module itself, not the working environment for its development).

Preparing for deployment


I do not describe how to create a database and configure a web server and PHP - for Magento 2 this is done in exactly the same way as for Magento 1. After setting up the system environment, you should have the parameters for working with the database and the URL, under your web application will run.

$ git clone git@github.com:flancer32/sample_mage2_module.git $ cd sample_mage2_module/work/ 

Creating a configuration file with parameters specific to your application instance (working with the database, URL, etc.): `` `bash
$ cp templates.json.init templates.json
$ nano templates.json
{
"vars": {
"LOCAL_ROOT": "/ home / magento / instance / sample_mage2_module",
"CFG_ADMIN_FIRSTNAME": "Store",
"CFG_ADMIN_LASTNAME": "Admin",
"CFG_ADMIN_EMAIL": "admin@store.com",
"CFG_ADMIN_USERNAME": "admin",
"CFG_ADMIN_PASSWORD": "eUvE7Yid057Cqtq5CkA8",
"CFG_BASE_URL": " http://mage2.local.host.com/ ",
"CFG_BACKEND_FRONTNAME": "admin",
"CFG_DB_HOST": "localhost",
"CFG_DB_NAME": "magento2",
"CFG_DB_USER": "magento2",
"CFG_DB_PASSWORD": "JvPESKVSjXvZDrGk2gBe",
"CFG_LANGUAGE": "en_US",
"CFG_CURRENCY": "USD",
"CFG_TIMEZONE": "UTC",
"CFG_USE_REWRITES": "0",
"CFG_USE_SECURE": "0",
"CFG_USE_SECURE_ADMIN": "0",
"CFG_ADMI_USE_SECURITY_KEY": "0",
"CFG_SESSION_SAVE": "db"
}
}

  <a href="http://devdocs.magento.com/guides/v2.0/install-gde/install/cli/install-cli-install.html#instgde-install-cli-magento"></a>    Magento 2   <i>setup:install</i> (.  <a href="https://github.com/flancer32/sample_mage2_module/blob/master/work/cfg/bin/deploy/post_install.sh">./work/bin/deploy/post_install.sh</a>). <h4></h4>```bash $ composer install $ sh ./bin/deploy/post_install.sh 

Access points to web interfaces:


What we have


The deployed Magento application (see the ./work/htdocs/ directory) with the files of our module linked to it (from the ./work/vendor/flancer32/sample_mage2_module/src/ directory). Therefore, when changing the files of our module in the deployed Magento 2 application, the files in the ./vendor / ... directory that are under version control change in fact. It remains to configure your IDE to work with this repository:


... and you can work with the source code of our module in a more or less familiar way:


The number of modules that can be connected in this way to the developer sweep is large enough. Composer will pull them all out and put them in the ./work/vendor/ directory, all that remains is to connect the necessary repositories to the IDE, and all the necessary code is under version control.

Additions


The root directory ./work/htdocs/ for deploying a Magento2 application is created in ./work/composer.json : `` `bash
{
"scripts": {
"pre-install-cmd": [
"# Create root directory for the development Magento instance.",
"mkdir -p htdocs"
]
}
}

        <a href="https://github.com/flancer64/magento-composer-installer"> </a>  <a href="https://github.com/magento/magento-composer-installer">magento/magento-composer-installer</a>, <a href="https://github.com/magento/magento-composer-installer/pull/7">pull request</a>        (. <a href="https://github.com/flancer32/sample_mage2_module/blob/master/work/composer.json#L28">./work/composer.json</a>): ```bash { "repositories": [ { "type": "vcs", "url": "https://github.com/flancer64/magento-composer-installer" } ], "require": { "magento/magento-composer-installer": "dev-patch-1" } } 

Configuring the composer_plugin_templates plugin (see ./work/composer.json ): `` `bash
{
"extra": {
"praxigento_templates_config": [
"./templates.json",
"./cfg/templates.json"
]
}
}

   <i>php $LOCAL_ROOT/work/htdocs/bin/magento setup:install</i> <a href="http://devdocs.magento.com/guides/v2.0/install-gde/install/cli/install-cli-install.html#instgde-install-cli-magento"></a> <i>--use-sample-data</i>  ```bash composer require magento/sample-data:~1.0.0-beta 

in the ./work directory, the desired sample data is first downloaded for a long time (especially media), then installed for a long time, then everything collapses, and you need to start over, but without sample data. Perhaps, when the errors are corrected, it will be possible to fill in sample data, but for now, like this.

Source: https://habr.com/ru/post/268245/


All Articles