# Working with Magento modules

When developing Magento 2 back-end, all the functionality you would like to add must be located in a module. Magento 2 module may contain:

* Changes to admin looks
* Modifications to Rest API, GraphQL endpoints
* Modifications to Magento 2 routes (**not ScandiPWA routes**)
* [And more!](https://devdocs.magento.com/guides/v2.4/architecture/archi_perspectives/components/modules/mod_intro.html#arch-modules-overview)

{% hint style="danger" %}

### Heads up!

Do not modify the `vendor` folde&#x72;*\** (a place where Magento 2 logic appears to be), this folder is auto-generated for every user, any modifications will be erased during the next `composer install`.\
\
\&#xNAN;*\*  you can do it for debugging, but do not forget to revert changes*
{% endhint %}

## Creating Magento 2 modules

There are two main ways to create a module in Magento 2:

* [Manually in `app/code`](#creating-modules-in-app-code) - for project-specific modules
* [Symlinking into `vendor` using composer](#symlinking-with-composer) - for modules you intend to share

After that, you might need to create an initial file structure for your module. Take a look at [the official guide](https://devdocs.magento.com/videos/fundamentals/create-a-new-module/#make-sure-you-have-permission-to-create-files-and-folders-in-your-installation) to learn how to do that!

### Creating modules in `app/code`

If you are building a module to be used by a single project, you can start by creating a new folder using a pattern `<VENDOR>/<NAME>` in `app/code` folder. For example: `app/code/MyProject/MyModule`.

### Symlinking with composer

If you are planning to share this module with other developers on [ScandiPWA Marketplace](https://marketplace.scandipwa.com/) or [Magento Marketplace](https://marketplace.magento.com/) (or even within your company), opt-in to this approach. With it, you would need to define a `composer.json` for your package, and you might create it in any directory of your Magento 2 root (we prefer `localmodules` for example). Then, you need to symlink the package:

```bash
composer config repo.<MODULE NAME> add <PATH TO MODULE>
composer require <"name" FIELD FROM composer.json FILE>
```

{% hint style="info" %}
This approach is more complex for beginners. If you do not feel strong with `composer` it is probably better to create a module in `app/code` first, and then convert your module into a Composer one. See [this guide](https://devdocs.magento.com/guides/v2.4/extension-dev-guide/package/package_module.html) for more details.
{% endhint %}

## Installing Magento 2 modules

Again, there are two ways:

* Composer way
* The ZIP way

After the installation, the following commands must be executed to enable and run post-install scripts of the extensions:

```bash
# see registration.php file to get Magento module name
magento module:enable <MAGENTO MODULE NAME>
magento setup:upgrade
```

### Composer way of installing Magento 2 extensions

That's a very simple operation, which requires a single command:

```bash
composer require <COMPOSER PACKAGE NAME>
```

{% hint style="danger" %}

### Heads up!

If installing extensions from any Marketplace, or other [private composer repositories](https://getcomposer.org/doc/articles/authentication-for-private-packages.md#authentication-for-privately-hosted-packages-and-repositories) - you must make sure your credentials are valid and set. CMA uses `COMPOSER_AUTH` environmental variable to authenticate, but there are [more ways to set the credentials](https://getcomposer.org/doc/articles/authentication-for-private-packages.md#authentication-using-the-composer-auth-environment-variable). **Usually, these credentials are provided by the marketplace in my account section.**
{% endhint %}

### Installing Magento 2 extensions with ZIP archives

All you need to do is extract the ZIP archive of an extension to `app/code` direcory of your Magento 2 project. Make sure the created folder matches a pattern `<VENDOR>/<NAME>`. For example: `app/code/OtherVendor/OtherModule`.
